简体   繁体   中英

Not getting default route parameter in ZF3

In ZF3 I want to get default parameter from 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 in my $params variable. $params变量。
In 2) I'm getting but I'm expecting default (in this case ) 但我希望使用默认值(在这种情况下,

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. So when you call somedomain/admin/color , it is the route /admin/:action which is selected.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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