2008-10-02 9 views
12

Kohana/CodeIgniter में, मैं एक यूआरएल इस रूप में हो सकता है:उपयोग यूआरएल क्षेत्रों

http://www.name.tld/controller_name/method_name/parameter_1/parameter_2/parameter_3 ... 

और इस प्रकार है तो मेरे नियंत्रक में पैरामीटर पढ़ें:

class MyController 
{ 
    public function method_name($param_A, $param_B, $param_C ...) 
    { 
     // ... code 
    } 
} 

आप इसे ज़ेंड फ्रेमवर्क में कैसे प्राप्त करते हैं?

उत्तर

6

अद्यतन (2016/04/13): नीचे चले गए और तय किया गया है मेरा उत्तर में लिंक। हालांकि, सिर्फ मामले में यह फिर से गायब हो जाता है - यहाँ कुछ विकल्प है कि इस तकनीक पर गहराई से जानकारी में कुछ प्रदान कर रहे हैं, साथ ही संदर्भ सामग्री के रूप मूल लेख का उपयोग करें:


@Andrew Taylor की प्रतिक्रिया यूआरएल पैरामीटर को संभालने का उचित ज़ेंड फ्रेमवर्क तरीका है। हालांकि, अगर आप अपने नियंत्रक की कार्रवाई में यूआरएल पैरामीटर रखना चाहते हैं (जैसा कि आपके उदाहरण में है) - this tutorial on Zend DevZone देखें।

11

Zend_Controller_Router वर्गों पर एक नज़र डालें:

http://framework.zend.com/manual/en/zend.controller.router.html

ये आप एक Zend_Controller_Router_Route जो तरीका है कि आप की जरूरत में अपने यूआरएल के लिए नक्शे को परिभाषित करने की अनुमति देगा।

सूचकांक नियंत्रक के सूचकांक कार्रवाई के लिए 4 स्थिर पैरामीटर होने का एक उदाहरण है: के बाद आप अपने सामने नियंत्रक द्वारा निर्धारित किए गए

$router = new Zend_Controller_Router_Rewrite(); 

$router->addRoute(
    'index', 
    new Zend_Controller_Router_Route('index/index/:param1/:param2/:param3/:param4', array('controller' => 'index', 'action' => 'index')) 
); 

$frontController->setRouter($router); 

यह आपके बूटस्ट्रैप में जोड़ा जाता है।

एक बार आपके कार्रवाई में, आप उसका उपयोग कर सकते हैं:

$this->_request->getParam('param1'); 

अपनी कार्रवाई विधि के अंदर मूल्यों तक पहुँचने के लिए।

एंड्रयू

+0

नाइस, डॉक्स संबंध में इतना स्पष्ट नहीं कर रहे हैं। –

1

मूलतः यहां पोस्ट http://cslai.coolsilon.com/2009/03/28/extending-zend-framework/

मेरे वर्तमान समाधान इस प्रकार है:

abstract class Coolsilon_Controller_Base 
    extends Zend_Controller_Action { 

    public function dispatch($actionName) { 
     $parameters = array(); 

     foreach($this->_parametersMeta($actionName) as $paramMeta) { 
      $parameters = array_merge( 
       $parameters, 
       $this->_parameter($paramMeta, $this->_getAllParams()) 
      ); 
     } 

     call_user_func_array(array(&$this, $actionName), $parameters); 
    } 

    private function _actionReference($className, $actionName) { 
     return new ReflectionMethod( 
      $className, $actionName 
     ); 
    } 

    private function _classReference() { 
     return new ReflectionObject($this); 
    } 

    private function _constructParameter($paramMeta, $parameters) { 
     return array_key_exists($paramMeta->getName(), $parameters) ? 
      array($paramMeta->getName() => $parameters[$paramMeta->getName()]) : 
      array($paramMeta->getName() => $paramMeta->getDefaultValue()); 
    } 

    private function _parameter($paramMeta, $parameters) { 
     return $this->_parameterIsValid($paramMeta, $parameters) ? 
      $this->_constructParameter($paramMeta, $parameters) : 
      $this->_throwParameterNotFoundException($paramMeta, $parameters); 
    } 

    private function _parameterIsValid($paramMeta, $parameters) { 
     return $paramMeta->isOptional() === FALSE 
      && empty($parameters[$paramMeta->getName()]) === FALSE; 
    } 

    private function _parametersMeta($actionName) { 
     return $this->_actionReference( 
       $this->_classReference()->getName(), 
       $actionName 
      ) 
      ->getParameters(); 
    } 

    private function _throwParameterNotFoundException($paramMeta, $parameters) { 
     throw new Exception(”Parameter: {$paramMeta->getName()} Cannot be empty”); 
    } 
} 
3

एक सरल तरीका है कि और अधिक जटिल विन्यास के लिए अनुमति देता है के लिए, this post प्रयास करें।सारांश में:

बनाएं application/configs/routes.ini

routes.popular.route = popular/:type/:page/:sortOrder 
routes.popular.defaults.controller = popular 
routes.popular.defaults.action = index 
routes.popular.defaults.type = images 
routes.popular.defaults.sortOrder = alltime 
routes.popular.defaults.page = 1 
routes.popular.reqs.type = \w+ 
routes.popular.reqs.page = \d+ 
routes.popular.reqs.sortOrder = \w+ 

निम्नलिखित bootstrap.php

// create $frontController if not already initialised 
$frontController = Zend_Controller_Front::getInstance(); 

$config = new Zend_Config_Ini(APPLICATION_PATH . ‘/config/routes.ini’); 
$router = $frontController->getRouter(); 
$router->addConfig($config,‘routes’); 
+0

लेकिन फिर मुझे प्रत्येक नियंत्रक के लिए कॉन्फ़िगर करना होगा: डी – Jeffrey04

+0

जो अक्सर उपयोगी होता है, मुझे नियंत्रक और मार्ग समूह अच्छी तरह से मिलते हैं अर्थात्/पृष्ठ/प्राप्त/1 और/उपयोगकर्ता/एंडी/पुष्टिकरण/ईमेल-पुष्टिकरण-टोकन इत्यादि। हालांकि बड़े अनुप्रयोगों के लिए यह अनावश्यक हो सकता है – Andy

4

मै अपने नियंत्रक वर्ग के साथ Zend_Controller_Action बढ़ाया और बना दिया है परिवर्तन में जोड़ें:

dispatch($action) में विधि प्रतिस्थापित

$this->$action();

call_user_func_array(array($this,$action), $this->getUrlParametersByPosition());

साथ

और मेरे कार्रवाई में तरह /mycontroller/myaction/123/321

एक यूआरएल के लिए अब जोड़ा निम्न विधि

/** 
* Returns array of url parts after controller and action 
*/ 
protected function getUrlParametersByPosition() 
{ 
    $request = $this->getRequest(); 
    $path = $request->getPathInfo(); 
    $path = explode('/', trim($path, '/')); 
    if(@$path[0]== $request->getControllerName()) 
    { 
     unset($path[0]); 
    } 
    if(@$path[1] == $request->getActionName()) 
    { 
     unset($path[1]); 
    } 
    return $path; 
} 

मैं नियंत्रक के बाद सभी पैरामीटर मिल जाएगा और कार्रवाई

public function editAction($param1 = null, $param2 = null) 
{ 
    // $param1 = 123 
    // $param2 = 321 
} 

यूआरएल में अतिरिक्त पैरामीटर किसी भी त्रुटि का कारण नहीं बनेंगे क्योंकि आप परिभाषित विधि के लिए अधिक पैरा भेज सकते हैं। आप उन्हें func_get_args() से प्राप्त कर सकते हैं और आप अभी भी getParam() सामान्य तरीके से उपयोग कर सकते हैं। आपके यूआरएल में डिफ़ॉल्ट का उपयोग कर एक्शन नेम नहीं हो सकता है।

असल में मेरे यूआरएल में पैरामीटर नाम नहीं हैं। केवल उनके मूल्य। (जैसा कि यह प्रश्न में था) और आपको फ्रेमवर्क की अवधारणाओं का पालन करने और ज़ेंड विधियों का उपयोग करके यूआरएल बनाने में सक्षम होने के लिए यूआरएल में पैरामीटर पदों को निर्दिष्ट करने के लिए मार्गों को परिभाषित करना होगा। लेकिन यदि आप हमेशा यूआरएल में अपने पैरामीटर की स्थिति जानते हैं तो आप इसे आसानी से प्राप्त कर सकते हैं।

यह प्रतिबिंब विधियों का उपयोग करने के रूप में परिष्कृत नहीं है लेकिन मुझे लगता है कि कम ओवरहेड प्रदान करता है।

डिस्पैच विधि अब इस तरह दिखता है:

/** 
* Dispatch the requested action 
* 
* @param string $action Method name of action 
* @return void 
*/ 
public function dispatch($action) 
{ 
    // Notify helpers of action preDispatch state 
    $this->_helper->notifyPreDispatch(); 

    $this->preDispatch(); 
    if ($this->getRequest()->isDispatched()) { 
     if (null === $this->_classMethods) { 
      $this->_classMethods = get_class_methods($this); 
     } 

     // preDispatch() didn't change the action, so we can continue 
     if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) { 
      if ($this->getInvokeArg('useCaseSensitiveActions')) { 
       trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"'); 
      } 
      //$this->$action(); 
      call_user_func_array(array($this,$action), $this->getUrlParametersByPosition()); 
     } else { 
      $this->__call($action, array()); 
     } 
     $this->postDispatch(); 
    } 

    // whats actually important here is that this action controller is 
    // shutting down, regardless of dispatching; notify the helpers of this 
    // state 
    $this->_helper->notifyPostDispatch(); 
}  
+0

अरे, अच्छा लग रहा है :) – Jeffrey04

संबंधित मुद्दे