如何在Zend Framework 2中使用路径传递params?

Pat*_*cow 1 php routes params zend-framework2

我有一个路由器test/view,我想通过一些类似的test/view/id/123.

我不知道如何在zf2路由器中添加这些参数.

'router' => array(
        'routes' => array(
            'test' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/test',
                    'defaults' => array(
                        '__NAMESPACE__' => 'test\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'view' => array(
                        'type' => 'Literal',
                        'options' => array(
                            'route' => '/view',
                            'defaults' => array(
                                'controller' => 'Index',
                                'action'     => 'view',
                            ),
                        ),
                    ),
                ),
            ),
        ),
),
Run Code Online (Sandbox Code Playgroud)

我设置view为儿童路线,但不知道在哪里添加这些参数.

我试过'route' => '/view/:id/:value'

'defaults' => array(
    'controller' => 'Index',
    'action'     => 'view',
    'id'         => 'value',
)
Run Code Online (Sandbox Code Playgroud)

但他们似乎没有工作

我试图了解这一切是如何运作的.

有任何想法吗?谢谢

peb*_*bbo 5

'router' => array(
    'routes' => array(
        'test-view' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/test/view/:testId[/]',
                'constraints' => array(
                    'testId'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Test\Controller\Index',
                    'action' => 'view'
                ),
            ),
        ),
Run Code Online (Sandbox Code Playgroud)

在您的ControllerAction中,您可以使用以下命令获取参数"testId":

$this->params('testId');
Run Code Online (Sandbox Code Playgroud)

顺便说一句:上面的路线给你一个这样的网址:/test/view/123- 我以为你可以摆脱"id"的参数.

如果要创建指向此类页面的链接,可以$this->url('test-view', array('testId' => 123))在其中一个视图中使用.