S Samir
S Samir

Reputation: 11

Handle Method not allowed in slim 3

Need you help to figure this out. i developed my website in Slim 3 framework. I wanted to handle "Method not allowed. Must be one of: POST" message which i get when i am using browser back and forward buttons.

I want to redirect to a different page when if the route is post and when user clicks on browser back or forward page.

When the post route is called is there a way where i can detect the that it is post method call and redirect him to a different get route.

Upvotes: 0

Views: 417

Answers (2)

NM Naufaldo
NM Naufaldo

Reputation: 1160

Another syntax solution

$notAllowedHandler = function ($c) {
    return function ($request, $response) use ($c) {
        return $response
            ->withJson(
                [
                    "status" => false,
                    "message" => "Your custom message",
                    "data" => [],
                ]
            )
            ->withStatus(400);
    };
};

$app = new App(
    [
        'notAllowedHandler' => $notAllowedHandler,
    ]
);

Upvotes: 0

zedling
zedling

Reputation: 637

You can add your own handler for specific errors:

$container['notAllowedHandler'] = function (ServerRequestInterface $request, ResponseInterface $response, array $methods) {
    // you can return a redirect response
};

see more here

Upvotes: 2

Related Questions