Laravel 5: Custom abort() message

2020-06-08 23:24发布

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.

7条回答
beautiful°
2楼-- · 2020-06-08 23:49

In Handler.php I altered the function:

 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;
}
查看更多
登录 后发表回答