Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have extended <code>Zend_Controller_Action</code> with my controller class and made the following changes:</p> <p>In <code>dispatch($action)</code> method replaced </p> <p><code>$this-&gt;$action();</code></p> <p>with</p> <p><code>call_user_func_array(array($this,$action), $this-&gt;getUrlParametersByPosition());</code></p> <p>And added the following method</p> <pre><code>/** * Returns array of url parts after controller and action */ protected function getUrlParametersByPosition() { $request = $this-&gt;getRequest(); $path = $request-&gt;getPathInfo(); $path = explode('/', trim($path, '/')); if(@$path[0]== $request-&gt;getControllerName()) { unset($path[0]); } if(@$path[1] == $request-&gt;getActionName()) { unset($path[1]); } return $path; } </code></pre> <p>Now for a URL like <code>/mycontroller/myaction/123/321</code></p> <p>in my action I will get all the params following controller and action</p> <pre><code>public function editAction($param1 = null, $param2 = null) { // $param1 = 123 // $param2 = 321 } </code></pre> <p>Extra parameters in URL won't cause any error as you can send more params to method then defined. You can get all of them by <code>func_get_args()</code> And you can still use <code>getParam()</code> in a usual way. Your URL may not contain action name using default one. </p> <p>Actually my URL does not contain parameter names. Only their values. (Exactly as it was in the question) And you have to define routes to specify parameters positions in URL to follow the concepts of framework and to be able to build URLs using Zend methods. But if you always know the position of your parameter in URL you can easily get it like this. </p> <p>That is not as sophisticated as using reflection methods but I guess provides less overhead. </p> <p>Dispatch method now looks like this:</p> <pre><code>/** * Dispatch the requested action * * @param string $action Method name of action * @return void */ public function dispatch($action) { // Notify helpers of action preDispatch state $this-&gt;_helper-&gt;notifyPreDispatch(); $this-&gt;preDispatch(); if ($this-&gt;getRequest()-&gt;isDispatched()) { if (null === $this-&gt;_classMethods) { $this-&gt;_classMethods = get_class_methods($this); } // preDispatch() didn't change the action, so we can continue if ($this-&gt;getInvokeArg('useCaseSensitiveActions') || in_array($action, $this-&gt;_classMethods)) { if ($this-&gt;getInvokeArg('useCaseSensitiveActions')) { trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"'); } //$this-&gt;$action(); call_user_func_array(array($this,$action), $this-&gt;getUrlParametersByPosition()); } else { $this-&gt;__call($action, array()); } $this-&gt;postDispatch(); } // whats actually important here is that this action controller is // shutting down, regardless of dispatching; notify the helpers of this // state $this-&gt;_helper-&gt;notifyPostDispatch(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload