如何在 Laravel 中发送带有中止的消息

how to send message with abort in Laravel

如果签名无效,我必须中止某些操作,而且我还想向呈现它的视图发送一条消息。我记得一年前在我之前的一个项目中能够做这样的事情。但是现在当我尝试这个时

    if (!$request->hasValidSignature()) {
        abort(401, 'Signature is not valid.');
    }

我收到一条错误消息,提示 $message 不可用

这是中止函数的定义,很明显它将消息作为输入。

public function abort($code, $message = '', array $headers = [])
{
    if ($code == 404) {
        throw new NotFoundHttpException($message);
    }

    throw new HttpException($code, $message, null, $headers);
}

我想知道为什么我会收到此错误:

ErrorException (E_ERROR)
Undefined variable: message (View: 
C:\laragon\www\laraone\resources\views\errors1.blade.php)

这是我的 401.blade.php 的一部分,它使用了那个变量...

<div class="content">
    <div class="title">401</div>Unauthorized action, {{ $message }}
</div>

使用$exception->getMessage()。来自 docs:

The HttpException instance raised by the abort function will be passed to the view as an $exception variable: <h2>{{ $exception->getMessage() }}</h2>

所以在你的情况下:

<div class="content">
    <div class="title">401</div>Unauthorized action, {{ $exception->getMessage() }}
</div>