1

在 ZF3 中,我想从路由中获取默认参数。我在控制器中以这种方式获取参数:

$params = $this->params()->fromRoute('crud');

我的网址如下所示:

1: somedomain/admin/color/add
2: somedomain/admin/color

1)我进入add我的$params变量。
在2)我得到null但我期待默认(在这种情况下view

我认为这是路由器配置错误的问题。

'admin' => [
            'type' => Segment::class,
            'options' => [
                'route' => '/admin/:action',
                'defaults' => [
                    'controller' => Controller\AdminController::class,
                    'action' => 'index',
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [
                'color' => [
                    'type' => Segment::class,
                    'options' => [
                        'route' => '/:crud',
                        'constraints' => [
                            'crud' => 'add|edit|delete|view',
                        ],
                        'defaults' => [
                            'controller' => Controller\AdminController::class,
                            'crud' => 'view',
                        ],
                    ],
                ],
            ],
            ],
4

1 回答 1

2

在您的路由定义中,您没有说路由器您的crud参数是可选的。所以当你打电话时somedomain/admin/color,它是被选择的路线/admin/:action

要指定可选参数,请使用括号表示法(假设您使用相同的操作):

'admin' => [
    'type' => Segment::class,
    'options' => [
        'route' => '/admin/:action[/:crud]',
        'defaults' => [
            'controller' => Controller\AdminController::class,
            'action' => 'index',
            'crud' => 'view',
        ],
        'constraints' => [
            'crud' => 'add|edit|delete|view',
        ],
    ],
],
于 2016-12-08T19:20:08.107 回答