Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrect routing for a Rest API with Zend
    text
    copied!<p>I'm trying to implement a REST API to my website.</p> <p>My problem is that the default Zend routing gets in the way. I've first tried using Zend_Rest_Route but I haven't been able to understand how I was supposed to use it correctly for "deep" routes, aka website/api/resource1/filter/resource2/id.</p> <p>Using the default Zend routing, I'd need to create a gigantic Resource1Controller to take care of all the possible actions, and I don't think it's the "good" way to do this.</p> <p>I've tried using Resauce ( <a href="http://github.com/mikekelly/Resauce/" rel="nofollow noreferrer">http://github.com/mikekelly/Resauce/</a>), creating an api module and adding routes, but I'm not able to get it working correctly : </p> <p>The patterns I added were : </p> <pre><code> $this-&gt;addResauceRoutes(array( 'api/resource' =&gt; 'resource', 'api/resource/:id' =&gt; 'custom', 'api/resource/filter' =&gt; 'resource-filter', 'api/resource/filter/:id' =&gt; 'custom', )); </code></pre> <p>Which then leads to this : </p> <pre><code>public function addResauceRoutes($routes) { $router = Zend_Controller_Front::getInstance()-&gt;getRouter(); foreach ($routes as $pattern =&gt; $controller) { $router-&gt;addRoute($controller, new Zend_Controller_Router_Route($pattern, array( 'module' =&gt; 'api', 'controller' =&gt; $controller ) ) ); } Zend_Controller_Front::getInstance()-&gt;setRouter($router); </code></pre> <ul> <li>website/api/resource gets me the Resource1Controller, ok</li> <li>website/api/resource/filter gets me to the resource1filterController, ok</li> <li>website/api/resource/filter/:id gets me to a custom controller, ok</li> <li>I'd like for website/api/resource/:id to get me to the same custom controller... But it redirects me to the Resource1Controller.</li> </ul> <p>What solution is there for me to correctly create my API ? Is there a good way to do this with Zend_Rest_Route ?</p> <hr> <p>Edit : Mike,</p> <p>I felt that it was not appropriate for me to use different controllers since I need the pathes "website/api/resource/:id" and "website/api/resource/filter/:id" to give me almost the exact same result (the only difference is that because the filter is there, I may get a message telling "content filtered" here). </p> <p>I thought it was a waste creating another almost identical controller when I could've used the same controller and just checked if a parameter "filter" was present. </p> <p>However, I don't want to use the basic Zend routing since for the path "website/api/resource/filter/resource2" I'd like to have a totally different comportment, so I'd like to use another controller, especially since I'm trying to use Zend_Rest_Action and need my controllers to use the basic actions getAction(), putAction(), postAction() and deleteAction().</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