ZF3: Как направить на конкретный контроллер / действие в зависимости от метода и маршрута?

В моем модуле module.config.phpУ меня есть что-то вроде этого:

namespace Application;

return [
//...
// myroute1 will route to IndexController fooAction if the route is matching '/index/foo' but regardless of request method
'myroute1' => [
'type' => Zend\Router\Http\Literal::class,
'options' => [
'route'    => '/index/foo',
'defaults' => [
'controller' => Controller\IndexController::class,
'action'     => 'foo',
],
],
],

// myroute2 will route to IndexController fooAction if the route is request method is GET but regardless of requested route
'myroute2' => [
'type'    => Zend\Router\Http\Method::class,
'options' => [
'verb'     => 'get',
'defaults' => [
'controller'    => Controller\IndexController::class,
'action'        => 'foo',
],
],
],
//...
];

Чего я пытаюсь добиться:

  • Если маршрут / Индекс / Foo запрашивается И запрашивается ПОЛУЧИТЬ метод, то он должен быть направлен на IndexController fooAction
  • Если маршрут / Индекс / Foo запрашивается И запрашивается СООБЩЕНИЕ метод, то он должен быть направлен на IndexController бардействие (обратите внимание, что это barAction здесь, а не fooAction)

Как этого добиться?

0

Решение

Попробуйте изменить литерал на Zend\Mvc\Router\Http\Part маршрут, а затем положить маршруты HTTP как маршруты CHILD!

Посмотреть здесь https://docs.zendframework.com/zend-router/routing/#http-route-types

2

Другие решения

Записка для себя и всех, кто ищет, как дополнительную заметку к ответу @ delboy1978uk.

Ответ, который я искал, выглядит примерно так:

  • ПОЛУЧИТЬ /index/foo => IndexController fooAction
  • СООБЩЕНИЕ /index/foo => 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'
],
],
]
],
],
//...
];
2

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector