【问题标题】:lumen throw 405 Method Not Allowed on get routelumen throw 405 Method Not Allowed on get route
【发布时间】:2017-09-30 11:22:38
【问题描述】:

我无法理解为什么我得到 405 以下

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

post url 按预期工作,但是当我定义 get route 时,应用程序开始向我抛出 405 。

调用url显示

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 工作正常,只是 get url 抛出 405...清除缓存,生成自动加载文件...不知道出了什么问题..

用新路由定义新控制器,它会抛出 404...我不认为这是一个路由问题,还有其他问题..

【问题讨论】:

  • 您是否正在向 get url 发出 GET 请求?输出看起来像您正在发布。
  • 是的,我正在做 GET 请求以使获取 url 工作。但输出是POST。认为这可能是 REST 客户端上的缓存问题,但..不确定这里可能出了什么问题

标签: laravel lumen lumen-5.3


【解决方案1】:

刚刚有同样的行为,花了大约一个小时试图解决它。

最后它是 GET 查询中的斜杠。

【讨论】:

    【解决方案2】:

    这是因为,您正在尝试访问具有 POST 方法的路由,或者您正在使用 POST 方法发布数据,以访问具有 GET 方法的路由。

    检查您的路线和表格。

    【讨论】:

      【解决方案3】:

      我按原样尝试了该场景并且它有效。你有调试吗?如果你进入.env文件,检查APP_DEBUG变量是否设置为true

      完成后,尝试加载页面并发布您看到的错误。

      PS:还要检查MyController控制器是否已经创建。

      【讨论】:

      • 我假设您正在访问路由:example.com/api/v1/my 如果是这样,并且您收到 404,请发布完整的错误。
      【解决方案4】:

      这是因为你的中间件没有处理OPTIONS请求

      你的中间件应该是这样的:

      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

      【讨论】:

        猜你喜欢
        • 2011-09-25
        • 1970-01-01
        • 2012-08-30
        • 2016-04-12
        • 1970-01-01
        • 2012-06-22
        • 2016-08-08
        • 2012-01-08
        • 2018-06-29
        相关资源
        最近更新 更多