user269867
user269867

Reputation: 3972

lumen throw 405 Method Not Allowed on get route

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..

Upvotes: 4

Views: 8312

Answers (4)

moolsbytheway
moolsbytheway

Reputation: 1292

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

Upvotes: 0

Denis Alexandrov
Denis Alexandrov

Reputation: 185

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

In the end it was trailing slash in GET query.

Upvotes: 4

Sunil tc
Sunil tc

Reputation: 2612

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.

Upvotes: 3

Jaimil Prajapati
Jaimil Prajapati

Reputation: 171

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.

Upvotes: 0

Related Questions