【问题标题】:Laravel 5 abort not returning error messageLaravel 5中止不返回错误消息
【发布时间】:2016-07-03 18:29:29
【问题描述】:

我试图从我的 Laravel 5.1 应用程序向我的前端 Angular 应用程序抛出一个错误。抛出的错误没有返回我需要的实际状态消息:

if (Hash::check($updated_profile['current_password'], $user->password)) {
    $user->password = Hash::make($updated_profile['new_password']);
    return $user;
} else {
    abort(500, 'Passwords do not match');
}

我也试过throw new \Exception('Passwords do not match');

对于这两者,我在前端收到错误,但它缺少状态消息:

Object
  data: null
  headers:(name)
  status: 0
  statusText:""
  __proto__: Object

【问题讨论】:

  • 您使用的是 API 包还是会扭曲您的控制器的东西?
  • 我用 oauth2 包包装

标签: php angularjs laravel laravel-5.1


【解决方案1】:

它在App class 中定义,所以如果你想用它来中止你当前的请求

首先在控制器顶部定义它

use App;

那你就可以这样使用这个功能了

App::abort(404,'The url not found');

【讨论】:

  • 告诉你在哪里使用它
  • 那也行不通。我还没有在 Laravel 5、6、7 或 8 中看到“消息”部分“开箱即用”。
【解决方案2】:

使用 response() 辅助方法而不是 abort()。语法如下:

return response("Passwords do not match", 500);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 2017-01-17
    • 2015-09-01
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多