简体   繁体   中英

Zend custom route not being recognized for controller

I'm creating a controller which will pull information of a painter from a database and display it: PainterController . I have defined it like so:

<?php
class PainterController extends Zend_Controller_Action
{

    public function init()
    {
        //setup painter route
        $router = $this->getFrontController()->getRouter();
        $router->addRoute(
            'painter',
            new Zend_Controller_Router_Route(
                'painter/:id',
                array(
                    'controller' => 'painter',
                    'action' => 'info'
                )
            )
        );
    }

    public function indexAction()
    {
        //index
    }

    public function infoAction()
    {
        //info was requested for given ID
    }
}
?>

As you can see, a route is setup to accept anything like domain.com/painter/12 where in this example the id 12 is passed to the infoAction.

Yet, when I visit such URL I the route is not being recognized, instead I get:

Message: Action "2" does not exist and was not trapped in __call()

Stack trace:

#0 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/library/Zend/Controller/Action.php(515): Zend_Controller_Action->__call('2Action', Array)
#1 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('2Action')
#2 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#3 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#4 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#5 /home/httpd/vhosts/xxx.com/subdomains/peter/httpdocs/public/index.php(25): Zend_Application->run()
#6 {main}  
Request Parameters:

array (
      'controller' => 'painter',
  'action' => '2',
  'module' => 'default',
)  

Does anyone have a clue on why this would happen?

(FYI, I realize that the files are located in a public directory, this is due to a shared webhost constraint. The files are protected using .htaccess. This is unrelated to the question.)

Update: it appears that the above does work when defined in the bootstrap. However, I do not like to put a lot of logic in the bootstrap. Is it possible to define controller related routes inside the controller itself?

Is it possible to define controller related routes inside the controller itself?

No. The router is responsible for finding which controller action to open. You have to add all your custom routes before the router is asked to route the request. This takes place in the front controller's dispatch method.

Besides the bootstrap file, you can add your routes to the application.ini or other router configuration files.

Also, you can add routes via a custom plugin. If your going to use a plugin. You must add to the routeStartup method because this comes immediately before the router routes the request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM