【问题标题】:ZF2 Routing as in ZF1ZF2 路由与 ZF1 相同
【发布时间】:2012-09-11 13:25:02
【问题描述】:

如何使路由自动适用于 ZF1 结构中的所有内容?

模块/控制器/动作/par1Name/par1Val/par2Name/par2Val/

我阅读了有关路由的信息,但在我看来,我必须手动添加所有操作,而且我发现可选参数有问题...

【问题讨论】:

    标签: zend-framework routing zend-framework2 zend-router


    【解决方案1】:

    您可以至少在每个控制器的基础上设置通配符 child_route,以获取类似 zf1 的路由:

    'products' => array(
                    'type' => 'Zend\Mvc\Router\Http\Segment',
                    'options' => array(
                        'route' => '/products[/:action]',
                        'defaults' => array(
                            'controller' => 'Application\Controller\Products',
                            'action' => 'index'
                        )
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'wildcard' => array(
                            'type' => 'Wildcard'
                        )
                    )
                )
    

    然后您可以使用例如 url() 视图助手:

    $this->url('products/wildcard',array('action'=>'edit','id'=>5,'foo'=>'bar');
    

    这将产生一个类似 /products/edit/id/5/foo/bar 的 url

    【讨论】:

    • the documentation 似乎这个方法现在已经被弃用了;还有其他解决方案吗?
    【解决方案2】:

    这是我用于从 ZF1 移植的所有内容的标准路由器 -> ZF2 请记住,您仍然需要将控制器添加为我在列表顶部的可调用对象。另外请记住,我总是将我的实际应用程序放在列表的底部,以便所有其他模块在它到达应用程序之前定义它们的路由。然而,这使得路由就像 ZF1 一样工作......我看到很多人问这个问题,所以我想我会发布!通过下面的设置,我可以添加我的新控制器(支持下面...)然后打开浏览器并转到... /support,它工作得很好。

    return array(
        'controllers' => array(
            'invokables' => array(
                'Application\Controller\Index' => 'Application\Controller\IndexController',
                'Application\Controller\Support' => 'Application\Controller\SupportController',
            ),
        ),
        'router' => array(
            'routes' => array(
                '*' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/[:controller[/:action]]',
                            /* OR add something like this to include the module path */
                            // 'route' => '/support/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            '__NAMESPACE__' => 'Application\Controller',
                            'controller'    => 'Index',
                            'action'        => 'index',
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'wildcard' => array(
                            'type' => 'Wildcard'
                        )
                    )
                ),
            ),
        ),
        'view_manager' => array(
            'display_not_found_reason' => true,
            'display_exceptions' => true,
            'doctype' => 'HTML5',
            'not_found_template' => 'error/404',
            'exception_template' => 'error/index',
            'template_map' => array(
                'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
                'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
                'error/404' => __DIR__ . '/../view/error/404.phtml',
                'error/index' => __DIR__ . '/../view/error/index.phtml',
            ),
            'template_path_stack' => array(
                __DIR__ . '/../view',
            ),
        ),   
        /* Service manager / translator etc stuff go HERE as they change less frequently */
    );
    

    我编辑了上面的路线以包含完整的模块路径作为我在初始帖子中遗漏的评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多