【问题标题】:Remove controller Index from URL - Zend Framework 1.12 and Routes从 URL 中删除控制器索引 - Zend Framework 1.12 和路由
【发布时间】:2014-05-06 10:36:31
【问题描述】:

在我的项目中,我在索引控制器中定义了一些页面,例如 ie。关于我们页面。为了让我不必输入domain.com/index/aboutdomain.com/about 我有这条路线:

$route = new Zend_Controller_Router_Route_Static ( 'about', array (
        'controller' => 'Index',
        'action' => 'about'
) );

$router->addRoute ( 'about', $route );

它完成了这项工作。问题是有时我有 6 或 7 页,我必须重复这条路线 6 或 7 次。有没有办法让我做一条总是从网址中删除“index”的路线?我永远不需要包含index 的网址。谢谢!

【问题讨论】:

    标签: php zend-framework routing zend-framework-mvc


    【解决方案1】:

    您可以通过避免静态路由类型来编写动态路由:

        $route = new Zend_Controller_Router_Route(
            '/:action', 
            array (
                'controller' => 'index',
                'action' => 'index',
            )
        );
        $router->addRoute('pages', $route);
    

    这将添加一个名为“pages”的路由,该路由将匹配索引控制器中的任何单个操作。此路由中定义的动作和控制器只是默认值,由于您没有将控制器作为变量传递,因此它将始终路由到 IndexController。该动作将默认为 indexAction 但可以被路由覆盖,即:

    /about -> IndexController / aboutAction
    /contact -> IndexController / contactAction
    etc ...
    

    请记住,这将覆盖任何其他路由,因此您需要正确构建路由层次结构。在此过程中稍后定义的路由将覆盖已定义的路由。

    有关更多信息,请查看文档:Zend Framework Standard Router

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      相关资源
      最近更新 更多