Zend Framework 路由转发

Zend Framework Routing Forward

我有一个旧的 url“/report/details/”,现在我想将这个 url 更改为“/customer_report/details”。控制器名称为 reportController,操作为 detailsAction。

我设置了 routes.ini 并添加了以下内容:

routes.report.route = /customer_report/details/
routes.report.defaults.controller = report
routes.report.defaults.action = details

编辑 routes.ini 后,如果我使用 url '/cutomer_report/details',它会完美运行。但是,如果我输入 url '/report/details/',它仍然有效。

有什么方法可以控制 url 为 /report/details 时,它会自动重定向到 /customer_report/details 吗?

PS。我正在使用 Zend Framework v1.12

如果不提供您的 ZF 版本信息,很难回答。 ZFv1 有像 /:controller/:action 这样的预定义路由,这就是为什么你的 /report/details 路由可能仍然有效。您可以更改此默认行为清除默认路由:

$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->removeDefaultRoutes(); 
$router->addRoute(
   'error', 
    new Zend_Controller_Router_Route (
       '/customer_report/details/',
       array (
          'controller' => 'report',
          'action' => 'details'
       )
    )
);

如果您想重定向用户,您可以将您的逻辑移动到一个新的操作:

public function customerReportAction() {
    // logic from reportAction()
}

并在旧版本中添加 http 重定向:

public function reportAction() {
   $this->_redirect('/customer_report/details/');
}