Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a quick example which should help you work with routes like that in ZF.</p> <p>If you are using a default project structure like the one you get when starting a new project using Zend Tool go into the Application folder.</p> <p>In your Bootstrap.php set up something like this:</p> <pre><code>protected function _initRouteBind() { // get the front controller and get the router $front = Zend_Controller_Front::getInstance(); $router = $front-&gt;getRouter(); // add each custom route like this giving them a descriptive name $router-&gt;addRoute( 'addTheDescriptiveRouteNameHere', new Zend_Controller_Router_Route( '/:controller/:id/:action/:somevar', array( 'module' =&gt; 'default', 'controller' =&gt; ':controller', 'action' =&gt; ':action', 'id' =&gt; ':id', 'somevar' =&gt; ':somevar' ) ) ); } </code></pre> <p>My example above is just to illustrate how you would use a route where the controller, the action and a couple of parameters are set in the url.</p> <p>The controller and action should be resolved without any additional work however to get the 'id' or 'somevar' value you can do this in your controller:</p> <pre><code>public function yourAction() { // How you get the parameters to pass in to a function $id = $this-&gt;getRequest()-&gt;getParam('id'); $somevar = $this-&gt;getRequest()-&gt;getParam('somevar'); // Using the parameters $dataYouWant = $this-&gt;yourAmazingMethod($id); $somethingElse = $this-&gt;yourOtherAmazingMethod($somevar); // Assign results to the view $this-&gt;view-&gt;data = $dataYouWant; $this-&gt;view-&gt;something = $somethingElse; } </code></pre> <p>So while you will want to make sure your methods handle the parameters being passed in with care (after all it is user supplied info) this is the principle behind making use of the route parameter binding. You can of course do things like '/site' as the route and have it direct to a CMS module or controller, then another for '/site/:id' where 'id' is a page identifier. </p> <p>Also just to say a nice alternative to: </p> <pre><code>$id = $this-&gt;getRequest()-&gt;getParam('id'); $somevar = $this-&gt;getRequest()-&gt;getParam('somevar'); </code></pre> <p>which wasn't that nice itself anyway since it was assuming a parameter was going to be passed, using shorthand conditional statement in conjunction with action helpers is:</p> <pre><code>$id = ($this-&gt;_hasParam('id')) ? $this-&gt;_getParam('id') : null; $somevar = ($this-&gt;_hasParam('somevar')) ? $this-&gt;_getParam('somevar') : null; </code></pre> <p>If you are not familiar with this, the </p> <pre><code>($this-&gt;_hasParam('id')) </code></pre> <p>is the conditional test which if true assigns the value of whatever is on the left of the ':' and which if false assigns the value of whatever is on the right of the ':'. </p> <p>So if true the value assigned would be </p> <pre><code>$this-&gt;_getParam('id') </code></pre> <p>and null if false. Does that help? :-D </p>
 

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