Php 缩短Zend框架路由定义
如何缩短Zend Framework中自定义路由的定义?我目前的定义是:Php 缩短Zend框架路由定义,php,zend-framework,routes,Php,Zend Framework,Routes,如何缩短Zend Framework中自定义路由的定义?我目前的定义是: $route = new Zend_Controller_Router_Route( ":module/:id", array( "controller" => "index", "action" => "index" ), array("id" => "\d+") ); self::$frontController->getRout
$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);
有没有办法更好地结合这些规则?
你的最佳实践是什么?当前,在前端控制器初始化之后,它们就在我的引导类中。在设置这样的路由时,我使用一个配置文件。作为首选,我使用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>
显然,还有其他选择,我鼓励您阅读关于此的内容,但是,这适合您的示例。我的routes.ini文件开始变得非常大,因此我决定在解析路由后使用Zend缓存来缓存路由。我使用Xcache作为后端缓存解决方案。下面是代码,应该放在Bootstrap.php文件中: protected function _initRoutes() { $backendType = 'Xcache'; $backendOptions = array(); // Instantiate a caching object for caching the routes $cache = Zend_Cache::factory('File', $backendType, array( 'automatic_serialization' => true, 'master_files'=>array(APPLICATION_PATH . '/configs/routes.ini') ), $backendOptions ); $frontController = Zend_Controller_Front::getInstance(); if(! $router = $cache->load('router')) { // Load up .ini file and put the results in the cache $routes = new Zend_Config_Ini (APPLICATION_PATH . '/configs/routes.ini', 'production'); $router = $frontController->getRouter(); $router->addConfig( $routes, 'routes' ); $cache->save($router, 'router'); } else { // Use cached version $frontController->setRouter($router); } } 受保护函数_initRoutes() { $backendType='Xcache'; $backendions=array(); //实例化用于缓存路由的缓存对象 $cache=Zend_cache::factory('File',$backendType, 排列( “自动_序列化”=>true, “主\u文件”=>数组(应用程序\u路径。“/configs/routes.ini”) ), $backendopions ); $frontController=Zend_Controller_Front::getInstance(); 如果(!$router=$cache->load('router')){ //加载.ini文件并将结果放入缓存 $routes=new Zend_Config_Ini(应用程序路径'/configs/routes.Ini','production'); $router=$frontController->getRouter(); $router->addConfig($routes,'routes'); $cache->save($router,'router'); } 否则{ //使用缓存版本 $frontController->setRouter($router); } }
我更喜欢使用*.ini文件而不是XMLs,尤其是在使用Zend时,因为它更像Zend,而且更轻、更紧凑。下面是一个使用
Zend\u Config\u Ini()
的类似配置
application.ini
[routes]
routes.shortcutone.route=:module/:id
routes.shortcutone.defaults.controller=index
routes.shortcutone.defaults.action=index
routes.shortcutone.reqs=\d+
bootstrap.php
$config = new Zend_Config_Xml('config.xml');
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addConfig($config, 'routes');
$config = new Zend_Config_Ini('application.ini', 'routes');
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addConfig($config, 'routes');
请注意,
application.ini
文件中的[routes]
部分可以重命名。重命名后,Zend\u Config\u Ini()
的第二个参数应该反映新的章节标题。谢谢。我将使用配置来保持我的引导程序精简:)嗨,我想知道如何缓存router对象,因为它处理FrontController,而FrontController本身处理不可序列化的PDO连接…routes.shortcutone.reqs.id=\d+
?