【问题标题】:Laravel 5.8 POST request always throws welcome pageLaravel 5.8 POST 请求总是抛出欢迎页面
【发布时间】:2019-07-24 15:45:49
【问题描述】:

我正在尝试使用邮递员测试我的 API,但是当我点击 POST 请求时,它总是将我带到欢迎页面。我已经在里面设置了 XSRF 令牌,但仍然无法正常工作。

这是我的api.php 路线:

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});
Route::resource('products/{product}/feedbacks', 'FeedbackController');

这是我来自FeedbackController的存储方法:

/**
 * Store a newly created resource in storage.
 *
 * @param  \App\Http\Requests\StoreFeedback  $request
 * @return \Illuminate\Http\Response
 */
public function store(Product $product, StoreFeedback $request)
{
   $product->addFeedback(
      $request->validated()
   );

   return response()->json([
      "status" => "success",
      "message" => "Your feedback has been submitted."
   ], 200);
}

这是我的web.php 文件:


Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::resource('product-categories', 'ProductCategoryController')->parameters([
    'product_category' => 'productCategory'
]);

Route::resource('product-sub-categories', 'ProductSubCategoryController')->parameters([
    'product_sub_category' => 'productCategory'
]);

这是我的邮递员请求的屏幕截图:Link to the screenshot

【问题讨论】:

  • 给你打电话请给我看你的index()函数?她返回了什么?
  • 请您尝试反转store()函数中的args,即:store(StoreFeedback $request, Product $product)
  • 这里,我上传给你看我所有的代码:link to code screenshot。如您所见,它甚至没有触及 index 方法。
  • @dparoli 已经试过了,还是一样
  • 您的web.php 文件是什么样的?有没有可能在其中定义了 /api 路由?

标签: php laravel


【解决方案1】:

我认为它显示相同的页面是因为您使用的是Route::resource,正如Question 中所说,

RESTful 资源控制器会为您设置一些默认路由,甚至命名它们。

Route::resource('users', 'UsersController');

为您提供这些命名路线:

Verb          Path                        Action  Route Name
GET           /users                      index   users.index
GET           /users/create               create  users.create
POST          /users                      store   users.store
GET           /users/{user}               show    users.show
GET           /users/{user}/edit          edit    users.edit
PUT|PATCH     /users/{user}               update  users.update
DELETE        /users/{user}               destroy users.destroy

我的猜测是:你基本上是一遍又一遍地在你的 FeedbackController 中调用 index() 函数

将您的路线更改为:

Route::post('products/{product}/feedbacks', 'FeedbackController@store');

EDIT将您的控制器功能更改为:

public function store(Request $request)
{
   dd($request->body); // or the key you send it on the postman
}

告诉我们你得到了什么

【讨论】:

  • U'uh.. 它把我扔到 index 方法而不是 store :/
  • 我忘了指出函数,它的:'FeedbackController@store'。我更新了我的答案。再试一次
  • 我知道,我在控制器上也添加了一个return $request;,但仍然没有任何反应。但是当我将其更改为 GET 时,它被解雇了:/ link to the screenshot
  • 这真的很奇怪,放个dd($request)看看你得到了什么
  • 当我这样做时它可以工作:Route::post('products/{product}/feedbacks', function () { dd("It does work"); });
【解决方案2】:

POST 请求总是抛出欢迎页面

问题在于您的App\Http\Requests\StoreFeedback 类。

如何?

因为您通过表单验证器传递请求。这使表单请求无效。这就是为什么将请求传递回之前的 URL,默认情况下变为 /

下面的层次结构

但是,如果你想得到错误,你可以简单地传递 HEADER Accept:application/json 来请求 HEADER,你就会得到错误。

原因:ValidationException handled here

【讨论】:

  • 我正要发这个!来自文档If validation fails, a redirect response will be generated to send the user back to their previous location.laravel.com/docs/5.7/validation
  • 从去年开始,我一直在探索laravel代码。这让我大开眼界。 :D
  • 感谢您的回答。我错过了验证的一个重要部分
【解决方案3】:

我刚刚发现了问题,它来自app\Http\Requests\StoreFeedback就像 Mr.ssi-anik 所说的。我不知道为什么要进行布尔验证,当我输入 truefalse 时,它会失败并将我重定向到欢迎页面。

我用的是0或者1,它接受参数并且工作正常。

【讨论】:

    【解决方案4】:

    如上所述,这是由于表单验证器造成的,您也可以使用中间件解决此问题,首先创建一个中间件:

    php artisan make:middleware JsonRequestMiddleware
    

    更新中间件的句柄方法

        public function handle(Request $request, Closure $next)
        {
            $request->headers->set("Accept", "application/json");
            return $next($request);
        }
    

    然后将此中间件添加到app/Http/Kernel

    protected $middleware = [
            ...
            \App\Http\Middleware\JsonRequestMiddleware::class,
           ...
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2015-08-15
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      • 2019-11-04
      • 2015-03-28
      相关资源
      最近更新 更多