ZF2中的ZF2路由

时间:2012-09-11 13:25:02

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

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

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

我阅读了有关路由的信息,但是我看到它的方式,我必须手动添加所有操作,并且我看到可选参数的问题......

2 个答案:

答案 0 :(得分:7)

您可以设置通配符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

这样的网址

答案 1 :(得分:6)

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

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 */
);

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