简体   繁体   English

在ZF3中未获取默认路由参数

[英]Not getting default route parameter in ZF3

In ZF3 I want to get default parameter from route. 在ZF3中,我想从route获取默认参数。 I'm getting parameters in this way in controller: 我在控制器中以这种方式获取参数:

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

My urls looks like this: 我的网址看起来像这样:

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

In 1) I'm getting add in my $params variable. 1)我要add $params变量。
In 2) I'm getting null but I'm expecting default (in this case view ) 2)我得到的是null但我希望使用默认值(在这种情况下, view

I think this is problem with bad router configuration. 我认为这是路由器配置错误的问题。

'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',
                        ],
                    ],
                ],
            ],
            ],

In your route definition, you didn't says the router that your crud parameter is optionnal. 在路由定义中,您没有说路由器的crud参数是可选的。 So when you call somedomain/admin/color , it is the route /admin/:action which is selected. 因此,当您调用somedomain/admin/color ,将选择路由/admin/:action

To specify a optional parameter, use the bracket notation (assuming you use the same 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',
        ],
    ],
],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM