30

これは私を夢中にさせています。私は Laravel 5 を使用していますが、4.2 のドキュメントと 404 ページの生成が機能しないようです。

まずglobal.phpがないので、routes.phpに以下を入れてみました。

App::missing(function($exception)
{
    return Response::view('errors.missing', array(), 404);
});

これにより、「method missing() not found」というエラーが発生します

デバッグは false に設定されています。

検索して検索しましたが、これまでのところ、Laravel 5 で 404 ページを設定する方法に関する情報は見つかりませんでした。

4

8 に答える 8

60

resources/views/errors に移動し、404 ページに必要なものを含む 404.blade.php ファイルを作成すると、残りは Laravel が処理します。

于 2015-02-06T16:44:44.113 に答える
14

グローバルなソリューションが必要な場合は、次のコードを追加して /app/Exceptions/Handler.php を変更できます

public function render($request, Exception $e)
{
    if ($this->isHttpException($e)) {

        $statusCode = $e->getStatusCode();

        switch ($statusCode) {

            case '404':
                return response()->view('layouts/index', [
                    'content' => view('errors/404')
                ]);
        }
    }
    return parent::render($request, $e);
}
于 2015-06-14T17:17:06.690 に答える
5

Laravel 5 では、カスタム404.blade.phpresources/views/errorsの下に置くだけで済みました。500 のようなその他のエラーについては、 app/Exceptions/Handler.phpで次を試すことができます。

public function render($request, Exception $e)
{

    if ( ! config('app.debug') && ! $this->isHttpException($e)) {
        return response()->view('errors.500');
    }

    return parent::render($request, $e);
}

そして、500 の HTTP 例外に対して同じことを行います

于 2015-09-14T21:39:03.103 に答える
2

Laravel 5 には、すでに app/Exceptions/Handler.php の下に事前定義された render メソッド (43 行目) があります。parent::render の前にリダイレクト コードを挿入するだけです。そのようです、

public function render($request, Exception $e)
{
    if ($e instanceof ModelNotFoundException) 
    {
        $e = new NotFoundHttpException($e->getMessage(), $e);
    }

    //insert this snippet
    if ($this->isHttpException($e)) 
    {
        $statusCode = $e->getStatusCode();
        switch ($statusCode) 
        {
            case '404': return response()->view('error', array(), 404);
        }
    }

    return parent::render($request, $e);
}

注: 私のビューはリソース/ビューの下にあります。どういうわけか、好きな場所に置くことができます。

于 2015-11-15T00:09:06.337 に答える