lumen throw 405 Method Not Allowed on get route

8.1k views Asked by At

I am not able to understand why I am getting 405 below

$app->group(['prefix' => 'api/v1'], function($app)
{
    $app->get('my','MyController@index');
    $app->post('my','MyController@store');
});

post url is working as expected but when I defined get route the application start throwing me 405 .

calling url show

in RoutesRequests.php line 596
at Application->handleDispatcherResponse(array(2, array('POST'))) in RoutesRequests.php line 533
at Application->Laravel\Lumen\Concerns\{closure}() in RoutesRequests.php line 781
at Application->sendThroughPipeline(array(), object(Closure)) in RoutesRequests.php line 534
at Application->dispatch(null) in RoutesRequests.php line 475
at Application->run() in index.php line 28

post url is working fine it's just the get url is throwing 405...cleared the cache , generated the autoload file...not sure what wrong..

Define new controller with new route and it throws 404...I am not seeing it as a route issue there is something else..

4

There are 4 answers

1
Jaimil Prajapati On

I tried out the scenario as-is and it works. Do you have debugging on? If you go in .env file, check if APP_DEBUG variable is set to true.

Once done, try loading the page and post the error you see.

PS: Also check if MyController controller has been created.

0
Sunil tc On

This is because, you are trying to access route which has POST method or you are posting data using POST method, to route which has GET method.

Check your route & form.

0
Denis Alexandrov On

Just had the same behaviour, spent like an hour trying to solve it.

In the end it was trailing slash in GET query.

0
moolsbytheway On

It's caused by your middleware is not handling OPTIONS requests

Your middleware should look like this :

class CorsMiddleware
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
    //Intercepts OPTIONS requests
    if ($request->isMethod('OPTIONS')) {
        $response = response('', 200);
    } else {
        // Pass the request to the next middleware
        $response = $next($request);
    }

    // Adds headers to the response
    $response->header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, PATCH, DELETE');
    $response->header('Access-Control-Allow-Headers', $request->header('Access-Control-Request-Headers'));
    $response->header('Access-Control-Allow-Origin', '*');
    $response->header('Access-Control-Expose-Headers', 'Location');

    // Sends it
    return $response;
}
}

https://github.com/laravel/lumen-framework/issues/674