4

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

user269867
  • 3,266
  • 9
  • 45
  • 65
  • are you making a GET request to the get url? output looks like you're POSTing. – patricus May 02 '17 at 22:37
  • yes I am doing GET request for get url to work. but the output is of POST. Thought it might be a cache issue on REST client but ..Not sure what might have been gone wrong in here – user269867 May 04 '17 at 16:12

4 Answers4

4

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

In the end it was trailing slash in GET query.

3

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.

Sunil tc
  • 2,482
  • 4
  • 19
  • 46
0

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

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

moolsbytheway
  • 1,152
  • 13
  • 20