Zend_Rest_Route

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Zend_Rest_Route

Joseph Crawford
Hello Everyone,

I have been trying to figure out the best method to accomlish a task and I have tried a Front Controller Plugin, along with extending Routes.  I was unable to use the FC Plugin because neither the routeStartup/routeShutdown would work for me.  I also am using the Zend_Rest_Route in conjunction with Zend_Rest_Controller.

Here is what I am trying to do.  I am creating a restful web api and each *version* will be it's own module.  When a user requests /2.0/controller/action I need to in this instance modify the request/route to change 2.0 into something ZF will allow for module names, currently I am using m2m0/.

I have been able to get this to work by extending the Zend_Rest_Route class in my own library which is at the pastebin.  However it seems like overkill to copy all this code to accomplish this small task.

http://pastebin.com/1F88qaQQ

If you have any suggestions on a better method or how I could achieve this without the code duplication I would really like to hear about it.

Thanks,
Joseph Crawford
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Rest_Route

Hector Virgen
I think creating a regex route along with modifying the request in route shutdown should do the trick. Have you tried that?

--
Hector


On Mon, Mar 8, 2010 at 11:29 AM, Joseph Crawford <[hidden email]> wrote:

Hello Everyone,

I have been trying to figure out the best method to accomlish a task and I
have tried a Front Controller Plugin, along with extending Routes.  I was
unable to use the FC Plugin because neither the routeStartup/routeShutdown
would work for me.  I also am using the Zend_Rest_Route in conjunction with
Zend_Rest_Controller.

Here is what I am trying to do.  I am creating a restful web api and each
*version* will be it's own module.  When a user requests
/2.0/controller/action I need to in this instance modify the request/route
to change 2.0 into something ZF will allow for module names, currently I am
using m2m0/.

I have been able to get this to work by extending the Zend_Rest_Route class
in my own library which is at the pastebin.  However it seems like overkill
to copy all this code to accomplish this small task.

http://pastebin.com/1F88qaQQ

If you have any suggestions on a better method or how I could achieve this
without the code duplication I would really like to hear about it.

Thanks,
Joseph Crawford
--
View this message in context: http://n4.nabble.com/Zend-Rest-Route-tp1585038p1585038.html
Sent from the Zend Framework mailing list archive at Nabble.com.

--
Hector Virgen
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Rest_Route

Joseph Crawford
In reply to this post by Joseph Crawford
I should note that in that pasted code the only lines that I have added are lines 32-36 which is why I think there has to be a better method.
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Rest_Route

Joseph Crawford
In reply to this post by Hector Virgen
I did try this with a normal route however I was told that you could not chain rest routes and I could not get it to work with

$regRoute->chain($restRoute);
$restRoute->chain($regRoute);

Do you know how I could get the chaining to work?  I need to keep the restfulness as well.

Thanks,
Joseph Crawford
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Rest_Route

Hector Virgen
Sorry I don't have experience with REST routes or chaining :( 

--
Hector


On Mon, Mar 8, 2010 at 11:43 AM, Joseph Crawford <[hidden email]> wrote:

I did try this with a normal route however I was told that you could not
chain rest routes and I could not get it to work with

$regRoute->chain($restRoute);
$restRoute->chain($regRoute);

Do you know how I could get the chaining to work?  I need to keep the
restfulness as well.

Thanks,
Joseph Crawford
--
View this message in context: http://n4.nabble.com/Zend-Rest-Route-tp1585038p1585075.html
Sent from the Zend Framework mailing list archive at Nabble.com.

--
Hector Virgen
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Rest_Route

Joseph Crawford
In reply to this post by Joseph Crawford
As suggested I have tried using a regular route in combination with a FC Plugin and implementing the routeShutdown method.  This works and it allows me to do the transformations that I require.  However the Zend_Rest_Route for some reason does not work like the standard routes and will not allow me to do transformations on the request object in the routeShutdown method.

I have also tried subclassing the Zend_Controller_Dispatcher_Standard and overriding the formatModuleName method.  This also did not work and should not have been tried because it never makes it past routing to get to this stage.  The routing cannot find a module named 2.0 so it just sets the module to be 'default' and the controller to be named 2.0.  This fails which leads to an exception.

So again I scratch my head and ask while using the Zend_Rest_Router how can I modify the module name prior to actually having the request routed?

Thanks,
Joseph Crawford
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Rest_Route

SpiffyJr
I had, at one point, modified the request URI to strip .xml, .html, and .json while setting the format parameter. You could modify the regex to do something similar (this is probably not the most efficient method).

        protected function _initCustomRequest()
        {
                $this->bootstrap('FrontController');
                $front = $this->getResource('FrontController');
               
                $matches = array();
                preg_match('/\.json|\.xml|\.html|\.rss/', $_SERVER['REQUEST_URI'], $matches);
               
                if (count($matches)) {
                        // Change request uri
                        $_SERVER['REQUEST_URI'] = str_replace($matches[0], '', $_SERVER['REQUEST_URI']);
                       
                        // Set default request
                        $request = $front->getRequest();
                        if (null === $front->getRequest()) {
                                $request = new Zend_Controller_Request_Http();
                                $front->setRequest($request);
                        }
                       
                        $front = Zend_Controller_Front::getInstance();
                        $front->getRequest()->setParam('format', strtolower(trim($matches[0], '.')));
                }
        }

Also, I have chained a rest route with a hostname route so I know it's possible to do chaining as well.

                $apiHostRoute = new Zend_Controller_Router_Route_Hostname('api.vld.dev', array('module' => 'api'));
                $apiRestRoute = new Zend_Rest_Route($front, array('module' => 'api'));
                $router->addRoute('api', $apiHostRoute->chain($apiRestRoute));
               
                $apiStaticRestRoute = new Zend_Rest_Route($front, array(), array('api'));
                $router->addRoute('apiStatic', $apiStaticRestRoute);

Kyle S
blogs @ www.spiffyjr.me
github @ www.github.com/spiffyjr
follow @ www.twitter.com/spiffyjr
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Rest_Route

Joseph Crawford
Kyle,

Thank you for the response.  Sorry that I could not reply sooner but I was in meetings all day.  Your method is a better way than I had extending the Zend_Rest_Route and duplicating about 100 lines of code.  I have implemented your way however I may end up moving that somewhere other than the bootstrap file in the future.

This is a great work-around but I do not think that it is solved in the framework itself.  I have reported a bug but so far there has not been any response to it.

http://framework.zend.com/issues/browse/ZF-9372
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Rest_Route

SpiffyJr
Glad I could help. I'm still not quite sure what bug you are referring to. Mind posting the JIRA link?
Kyle S
blogs @ www.spiffyjr.me
github @ www.github.com/spiffyjr
follow @ www.twitter.com/spiffyjr
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Rest_Route

Joseph Crawford
I posted a link to the issue in my last post, here it is again: http://framework.zend.com/issues/browse/ZF-9372