changing zend framework default action based on th

2019-05-25 20:50发布

问题:

So I know that you can set a custom default action in Zend Framework in the application.ini:

resources.frontController.defaultAction = "something"

But what if I want this default action to be contingent on the controller? So controller A's default action will be B, controller C's default action will be D, etc how do I configure the controllers to take these default action parameters? What piece of code is needed and where should I place them?

回答1:

You can add in your Bootstrap.php something like this:

   protected function _initRoutes()
   {
      $Router = Zend_Controller_Front::getInstance()->getRouter();

      $Route = new Zend_Controller_Router_Route(
                      '/controller1',
                      array(
                          'action' => 'customaction1'
                      )
      );
      $Router->addRoute('c1', $Route);

      $Route = new Zend_Controller_Router_Route(
                      '/controller2',
                      array(
                          'action' => 'customaction2'
                      )
      );
      $Router->addRoute('c2', $Route);

      [...]
   }


回答2:

An alternative approach to handling lots of routes could be to have the controller decide what he wants to do:

public function indexAction() {
  $this->_forward('mydefault');
}

public function mydefaultAction() {
  $this->view->message = 'I get called on /controller/index';
}

My GUESS is, that this approach is faster than adding dozens of custom routes. But that's just a guess, nothing tested here.



回答3:

By default you can only say in general what the default action for a controller is. If you want to dispatch a certain controller and action under a specific URI, you can do this by using routes.

There is a default route which you probably use in your application right now. You can add additional routes from the application.ini or otherwise create a frontController plugin to inject routes into the router. The former is much easier, the latter gives you more abilities, for example to load routes based on entries in the database.

The routes in application.ini are good explained in the manual of ZF. It comes down to these four lines per route:

resources.router.routes.route_id.route = "/login"
resources.router.routes.route_id.defaults.module = "user"
resources.router.routes.route_id.defaults.controller = "login"
resources.router.routes.route_id.defaults.action = "index"

Here it is possible to specify the default action by defaults.action. For parameters in routes, the section about the Zend_Controller_Router_Route type explains it pretty well.

The other option is a frontController plugin. This class can hook into different parts of the process and this system is very powerful if used correctly. It is probably not required for your question, but you might want to have a look at the manual where there is a page about plugins and a section how to load these plugins in your application.ini.