Zend框架不关心指定的Custom Route

发布于 2024-12-02 22:05:58 字数 647 浏览 2 评论 0原文

我创建了一个路由器并添加到控制器中,如下所示

public function _initRouting() {          
    // Get Front Controller Instance         
    $front = Zend_Controller_Front::getInstance();  
    // Get Router
    $router = $front -> getRouter();
    $routePage = new Zend_Controller_Router_Route('/page/:action/:cat/:parent/:id', array(
        'controller' => 'page',
        'action'    => 'list',
        'cat'       => 'general',
        'parent'    => '0',
        'module'    => 'default'
    ));
    $router -> addRoute('page', $routePage);
}

首先,每当我导航到 /page/list/general/0/1,它采用标准路线,而不是新路线。

I created a router and added to the controller like this

public function _initRouting() {          
    // Get Front Controller Instance         
    $front = Zend_Controller_Front::getInstance();  
    // Get Router
    $router = $front -> getRouter();
    $routePage = new Zend_Controller_Router_Route('/page/:action/:cat/:parent/:id', array(
        'controller' => 'page',
        'action'    => 'list',
        'cat'       => 'general',
        'parent'    => '0',
        'module'    => 'default'
    ));
    $router -> addRoute('page', $routePage);
}

First this router is not doing anything, whenever I navigation to
/page/list/general/0/1, it takes the standard route, not the new route.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹淡然 2024-12-09 22:05:58

我唯一能想到的是前端控制器资源在您的 init 方法之前尚未“引导”。

您至少应该引导并检索前端控制器资源

protected function _initRouting()
{
    $this->bootstrap('frontController');
    $front = $this->getResource('frontController');
    // etc

为什么不跳过创建引导初始化方法并在应用程序配置中配置路由器资源呢?

resources.router.routes.page.route = "page/:action/:cat/:parent/:id"
resources.router.routes.page.defaults.module = "default"
resources.router.routes.page.defaults.controller = "page"
resources.router.routes.page.defaults.action = "list"
resources.router.routes.page.defaults.cat = "general"
resources.router.routes.page.defaults.parent = "0"

作为测试,我添加了上述配置并使用此 listAction 创建了一个 PageController

public function listAction()
{
    Zend_Debug::dump($this->getRequest()->getParams());
    exit;
}

调用 page/list/general/0/1 产生

array(6) {
  ["action"] => string(4) "list"
  ["cat"] => string(7) "general"
  ["parent"] => string(1) "0"
  ["id"] => string(1) "1"
  ["module"] => string(7) "default"
  ["controller"] => string(4) "page"
}

The only thing I can think of is the front controller resource has not been "bootstrapped" prior to your init method.

You should at least bootstrap and retrieve the front controller resource

protected function _initRouting()
{
    $this->bootstrap('frontController');
    $front = $this->getResource('frontController');
    // etc

Why don't you just skip creating a bootstrap init method and configure the router resource in your application config?

resources.router.routes.page.route = "page/:action/:cat/:parent/:id"
resources.router.routes.page.defaults.module = "default"
resources.router.routes.page.defaults.controller = "page"
resources.router.routes.page.defaults.action = "list"
resources.router.routes.page.defaults.cat = "general"
resources.router.routes.page.defaults.parent = "0"

As a test, I added the above config and created a PageController with this listAction

public function listAction()
{
    Zend_Debug::dump($this->getRequest()->getParams());
    exit;
}

Calling page/list/general/0/1 yields

array(6) {
  ["action"] => string(4) "list"
  ["cat"] => string(7) "general"
  ["parent"] => string(1) "0"
  ["id"] => string(1) "1"
  ["module"] => string(7) "default"
  ["controller"] => string(4) "page"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文