如何缩短Zend Framework中自定义路由的定义?我目前有这个定义:
$route = new Zend_Controller_Router_Route( ":module/:id",array( "controller" => "index","action" => "index" ),array("id" => "\d+") ); self::$frontController->getRouter()->addRoute('shortcutOne',$route); $route = new Zend_Controller_Router_Route( ":module/:controller/:id",array("action" => "index"),array("id" => "\d+") ); self::$frontController->getRouter()->addRoute('shortcutTwo',$route); $route = new Zend_Controller_Router_Route( ":module/:controller/:action/:id",null,array("id" => "\d+") ); self::$frontController->getRouter()->addRoute('shortcutThree',$route);
有没有办法更好地结合这些规则?
你在哪里放置这些的最佳做法是什么?我正在Front Controller初始化之后,将它们放在我的引导类中.
当设置这样的路由时,我使用配置文件.作为首选,我使用XML存储我的配置数据,但是这些可以轻松地以另一种支持的格式存储.然后,我将路由从配置添加到我的引导中的路由器.
配置:
<config> <routes> <shortcutone type="Zend_Controller_Router_Route"> <route>:module/:id</route> <defaults> <controller>index</controller> <action>index</action> </defaults> <reqs id="\d+"> </shortcutone> <shortcuttwo type="Zend_Controller_Router_Route"> <route>:module/:controller/:id</route> <defaults> <controller>index</controller> </defaults> <reqs id="\d+"> </shortcuttwo> <shortcutthree type="Zend_Controller_Router_Route"> <route>:module/:controller/:action/:id</route> <defaults> <controller>index</controller> <action>index</action> </defaults> <reqs id="\d+"> </shortcutthree> </routes> </config>
引导
$config = new Zend_Config_Xml('config.xml'); $router = Zend_Controller_Front::getInstance()->getRouter(); $router->addConfig($config,'routes');
显然,还有其他的选择,我鼓励你阅读这个documentation,但是,这适合你的例子.