Zend Route Catch all

▼魔方 西西 提交于 2020-01-04 11:03:41

问题


Please help! I am a newbie to Zend and want to modifiy the default routing for a cms project I am working on.

How do I create a "catch all" route in zend should a controller not exist?

I am trying to create links like:

mydomain.com/slug

mydomain.com/slug1

Where slug and slug1 can be passed as params to a specified default controller (pagesController) so I can fetch the appropriate content from the DB.

I apprecaite any help!! :)


回答1:


One way to do it is to write a simple Controller Plugin that tests whether a request is otherwise dispatchable, and if not, send it to your page controller/action:

<?PHP
class PageRouter extends Zend_Controller_Plugin_Abstract {

  public function preDispatch(Zend_Controller_Request_Abstract $req) {
    $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
    if (!$dispatcher->isDispatchable($req, $req)) {

      $req->setModuleName('default');
      $req->setControllerName('page');
      $req->setActionName('page');
    }
  }

}

And make sure you register it with your frontcontroller:

Bootstrap.php:

protected function _initFrontControllerPlugins() {
    $this->bootstrap('FrontController');

    $fc = $this->getResource('FrontController');

    $pluginPageRouter = new PageRouter();
    $fc->registerPlugin($pluginPageRouter);    
}



回答2:


Instead of overriding the preDispatch, you could also do this in the routeShutdown. This was the only way to getting this up for me.



来源:https://stackoverflow.com/questions/4286877/zend-route-catch-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!