ZF3:如何根据方法和路由路由到特定的控制器/操作?

时间:2017-09-12 13:48:16

标签: php zend-framework zend-route zend-framework3 zend-router

在我的模块command中,我有类似的内容:

locals()

我想要实现的目标:

  • 如果请求路由 / index / foo 并且 GET 方法请求路由,那么它应该路由到 IndexController fooAction
  • 如果请求路由 / index / foo 并且 POST 方法请求路由,那么它应该路由到 IndexController < em> bar 行动(注意这里的barAction不是fooAction)

如何实现?

2 个答案:

答案 0 :(得分:2)

尝试将文字更改为Zend\Mvc\Router\Http\Part路由,然后将HTTP路由作为CHILD路由放入!

请参阅此处https://docs.zendframework.com/zend-router/routing/#http-route-types

答案 1 :(得分:2)

给自己和其他任何人看的一张纸条,作为@ delboy1978uk答案的附加说明。

我正在寻找的答案是这样的:

  • GET /index/foo =&gt; IndexController fooAction
  • POST /index/foo =&gt; IndexController barAction

因此module.config.php文件中的代码可以是这样的:

return [
    //...
    'myroute1' => [// The parent route will match the route "/index/foo"
        'type' => Zend\Router\Http\Literal::class,
        'options' => [
            'route'    => '/index/foo',
            'defaults' => [
                'controller' => Controller\IndexController::class,
                'action'     => 'foo',
            ],
        ],
        'may_terminate' => false,
        'child_routes' => [
            'myroute1get' => [// This child route will match GET request
                'type' => Method::class,
                'options' => [
                    'verb' => 'get',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'foo'
                    ],
                ],
            ],
            'myroute1post' => [// This child route will match POST request
                'type' => Method::class,
                'options' => [
                    'verb' => 'post',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'bar'
                    ],
                ],
            ]
        ],
    ],
    //...
];