php Laravel 5:自定义 abort() 消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38585485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5: Custom abort() message
提问by Sharon Haim Pour
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?
使用 Laravel 5,我想发送自定义abort()
消息。
例如,如果用户没有操作所需的权限,
我想要abort(401, "User can't perform this actions")
.
目前,当我这样做时,响应文本是 HTML 页面而不是消息。
我怎样才能只返回消息?
Note:I don't want to pass a different view, but only the custom message.
注意:我不想传递不同的视图,而只想传递自定义消息。
回答by Kenny
According to Laravel 5.4 documentation:
根据 Laravel 5.4 文档:
https://laravel.com/docs/5.4/errors#http-exceptions
https://laravel.com/docs/5.4/errors#http-exceptions
You can use abort
helper with response text:
您可以将abort
helper 与响应文本一起使用:
abort(500, 'Something went wrong');
And use $exception->getMessage()
in resources/views/errors/500.blade.php
to display it:
并使用$exception->getMessage()
inresources/views/errors/500.blade.php
来显示它:
Error message: {{ $exception->getMessage() }}
回答by ShaunUK
The answer is simply to use the response() helper method instead of abort(). Syntax as below.
答案只是使用 response() 辅助方法而不是 abort()。语法如下。
return response("User can't perform this action.", 401);
回答by xcitic
You can wrap a response inside abort, which will stop the execution and return the response. If you want it to be JSON then add ->json();
您可以在 abort 内包装响应,这将停止执行并返回响应。如果你希望它是 JSON 然后添加->json();
# Regular response
abort( response('Unauthorized', 401) );
# JSON response
abort( response()->json('Unauthorized', 401) );
回答by knorthfield
Should be able to use the following in the template:
应该能够在模板中使用以下内容:
{{ $exception->getMessage() }}
回答by vijaykumar
You can handle all error exceptions here app/Exceptions/Handler.php Class On your requirement.
您可以在 app/Exceptions/Handler.php Class 根据您的要求处理所有错误异常。
In your case just replace render function with this
在你的情况下,只需用这个替换渲染功能
public function render($request, Exception $e)
{
return $e->getMessage();
//For Json
//return response()->json(['message' => $e->getMessage()]);
}
回答by Ali
First of all add the error message in your header file or in the page where you want to show the message like:
首先在您的头文件或您想要显示消息的页面中添加错误消息,例如:
@if($errors->has())
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
@endif
And in the controller, you can do this kind of stuff (e.g.):
在控制器中,您可以执行以下操作(例如):
public function store(){
if(user is not permitted to access this action) // check user permission here
{
return redirect()->back()->withErrors("User can't perform this actions");
}
}
You can redirect back with error message
您可以使用错误消息重定向回来
回答by Sharon Haim Pour
In Handler.php
I altered the function:
在Handler.php
我改变了功能:
public function render($request, Exception $e)
{
$response = parent::render($request, $e);
if (method_exists($e, "getStatusCode")) {
if ($e->getStatusCode() == 401) {
$response->setContent($e->getMessage());
}
}
return $response;
}