zend-frameworkzend-routezend-restzend-rest-route

zend rest route messing up routing for default controller


I have implemented a RESTful service by extending the Zend_Rest_Controller. The service works great. I have only one controller that actually has RESTful behavior. So I added the rest route for just that controller in the bootstrap.

protected function _initRestRoute()
{
    $this->bootstrap('frontController');
    $frontController = Zend_Controller_Front::getInstance();
    $restRoute = new Zend_Rest_Route($frontController, array() , array('default' =>     array('MyserviceController')));
    $frontController->getRouter()->addRoute('rest', $restRoute);

}  

problem starts when I run my portal zend app. The links in the layout for the index controller miss out on the action parameter when I construct the URL. For example the link on the index layout for a action homepage in the network controller is as follows.

$this->url(array('controller'=>'network','action'=>'homepage','module'=>'default'));

this should return "localhost/projectname/public/network/homepage" instead it returns "localhost/projectname/public/network". This behavior is only when the layout is loaded by the default controller i.e. IndexController. The routing problem vanishes if I remove the zend rest route from the bootstrap. As long as I am mentioning what controller the rest request has to route to it should not be a problem right. But this is affecting the default controller routing.


Solution

  • This seems to be a common issue and usually resolved with something like:

    $this->url(array('controller'=>'network','action'=>'homepage','module'=>'default'), 'default');
    

    or:

    $this->url(array('controller'=>'network','action'=>'homepage','module'=>'default'), NULL, TRUE);
    

    might work.

    if your 'module'=>'default' is the default controllers directory at/application/controllers you can omit the module option from the route.

    Here is the whole url method note the comments for the reset option.

    /**
         * Generates an url given the name of a route.
         *
         * @access public
         *
         * @param  array $urlOptions Options passed to the assemble method of the Route object.
         * @param  mixed $name The name of a Route to use. If null it will use the current Route
         * @param  bool $reset Whether or not to reset the route defaults with those provided
         * @return string Url for the link href attribute.
         */
        public function url(array $urlOptions = array(), $name = null, $reset = false, $encode = true)
        {
            $router = Zend_Controller_Front::getInstance()->getRouter();
            return $router->assemble($urlOptions, $name, $reset, $encode);
        }