On Wed, Sep 9, 2009 at 11:36 PM, Matthew Weier O'Phinney
<matt...@zend.com>wrote:

> -- Cameron <themsel...@gmail.com> wrote
> (on Wednesday, 09 September 2009, 05:35 PM +0800):
> > Hi, this is very likely to be a dumb question, but I'd really like to
> have my
> > controllers respond to "normal" requests (as in
> http://domain.com/product/
> > display/id/1) and also the full range of RESTful requests (GET http://
> > domain.com/product/1 and so on). Turning on Zend_Rest_Route across all
> my
> > controllers breaks regular requests, but does make them respond correctly
> to
> > REST, but yeah... I know this is unlikely to work, I was just wondering
> if
> > there's a way of somehow ordering the routes so that if regular ones
> don't
> > match then use the RESTful ones or something. Failing that, I suppose I'm
> just
> > going to have to come up with a nice clean way to link the separate REST
> > controllers back to the "normal" code.
>
> You can do it in a variety of ways. First, you can always use the
> default route in conjunction with the Rest route, though you run the
> risk of the actions being interpreted as IDs.
>
> Another option is to create an additional Zend_Controller_Router_Route
> instance that matches your controller:
>
>    $route = new Zend_Controller_Router_Route(
>        'product/:action/*',
>        array(
>            'controller' => 'product',
>        ),
>        array(
>            'action' => '^[a-z][a-z0-9.-]+',
>        )
>    );
>
> Assuming your IDs are numeric, the above will not match unless the
> action starts with an alphabetic character -- which should be safe for
> your purposes. (You can also use static and regexp routes in this
> fashion.)
>
> If you use such a route, make sure you register it *after* the REST
> route to ensure it's executed *before* it.
>
>
Thanks Matthew, this works perfectly. For the record, here is the complete
Bootstrap method for setting up the routes in complete Zend Application
style. If you drop this in your application it should just work. Now to work
out how to get REST routes to respond with JSON and regular routes with
regular view scripts!

protected function _initRestRoute() {
        $this->bootstrap('Request');
        $front = $this->getResource('FrontController');
        $restRoute = new Zend_Rest_Route($front);
        $defaultRoute = new Zend_Controller_Router_Route(
            ':controller/:action/*', array('module' => 'default'),
array('action' => '^[a-z][a-z0-9.-]+')
        );
        $front->getRouter()->addRoute('rest', $restRoute);
        $front->getRouter()->addRoute('default', $defaultRoute);
    }

Reply via email to