phpzend-frameworkzend-routezend-framework3zend-router

ZF3: How to route to specific controller/action based on method and route?


In my module's module.config.php, I have something like this:

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',
            ],
        ],
    ],
    //...
];

What I'm trying to achieve:

How to achieve that?


Solution

  • Try changing the literal to a Zend\Mvc\Router\Http\Part route, and then putting the HTTP routes in as CHILD routes!

    See here https://docs.zendframework.com/zend-router/routing/#http-route-types