Configure route to accept only pont

Hello friends

it is possible to configure the method validation in the segment type route.
For example, I need my route to only accept POST methods.
My route is like this

'router' => [
        'routes' => [
            'cargo' => [
                'type'    => Segment::class,
                'options' => [
                    'route' => '/cargo[/:action[/:id]]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => Controller\CargoController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],    

Tks

I figure you’ll need to do this in the controller:

public function indexAction() {
if ($this->getRequest()->isPost()) {
//do the things happening when post method is used.
} else {
//throw an exception for everything else
throw new Exception("Method not supported");
}
}

This code snippet is laminas-mvc compatible, maybe there’s something alike in zend-mvc.

1 Like