簡體   English   中英

在ZF3中未獲取默認路由參數

[英]Not getting default route parameter in ZF3

在ZF3中,我想從route獲取默認參數。 我在控制器中以這種方式獲取參數:

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

在路由定義中,您沒有說路由器的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',
        ],
    ],
],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM