gpt4 book ai didi

php - 缩短 Zend 框架路由定义

转载 作者:可可西里 更新时间:2023-10-31 22:09:43 25 4
gpt4 key购买 nike

如何缩短 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);

有没有办法更好地结合这些规则?在放置这些内容方面,您的最佳做法是什么?我目前在前端 Controller 初始化后立即将它们放在我的 Bootstrap 类中。

最佳答案

我的 routes.ini 文件开始变得非常大,所以我决定在解析后使用 Zend Caching 来缓存路由。我使用 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);  }}

关于php - 缩短 Zend 框架路由定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/794126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com