Laravel 5:自定义abort()消息 [英] Laravel 5: Custom abort() message
问题描述
我要使用Laravel 5发送自定义abort()
消息.
例如,如果用户没有操作所需的权限,
我想abort(401, "User can't perform this actions")
.
当前,当我这样做时,响应文本是HTML页面,而不是消息.
如何仅返回邮件?
Using Laravel 5, I want to send a custom abort()
message.
For example, if the user doesn't have the required permissions for an action,
I'd like to abort(401, "User can't perform this actions")
.
Currently, when I do so, the response text is HTML page and not the message.
How can I return only the message?
注意:我不想传递其他视图,而只传递自定义消息.
Note: I don't want to pass a different view, but only the custom message.
推荐答案
根据Laravel 5.4文档:
According to Laravel 5.4 documentation:
https://laravel.com/docs/5.4/errors#http-exceptions
您可以将abort
帮助器与响应文本一起使用:
You can use abort
helper with response text:
abort(500, 'Something went wrong');
并在resources/views/errors/500.blade.php
中使用$exception->getMessage()
进行显示:
Error message: {{ $exception->getMessage() }}
这篇关于Laravel 5:自定义abort()消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!