【问题标题】:Laravel 5.1 custom 404 based on routeLaravel 5.1 自定义 404 基于路由
【发布时间】:2016-01-09 16:31:07
【问题描述】:

我是 laravel 的新手。我想根据请求的 url 显示自定义 404 错误。

示例:
site.com/somepage

site.com/admin/somepage

我已经看到 this 的答案,但它适用于 laravel 4。我找不到 Laravel 5.1 的等效答案

请帮忙。谢谢!


-- 答案--

我的代码基于Abdul Basit的建议

//Handle Route Not Found
if ($e instanceof NotFoundHttpException) {
    //Ajax Requests
    if ($request->ajax() || $request->wantsJson()) {
        $data = [
            'error' => true,
            'message' => 'The route is not defined',
        ];
        return Response::json($data, '404');
    } else {
        //Return view
        if (Request::is('admin/*')) {
            return Response::view('admin.errors.404', array(), 404);
        } else {
            return Response::view('errors.404', array(), 404);
        }
    }
}

//Handle HTTP Method Not Allowed
if ($e instanceof MethodNotAllowedHttpException) {
    //Ajax Requests
    if ($request->ajax() || $request->wantsJson()) {
        $data = [
            'error' => true,
            'message' => 'The http method not allowed',
        ];
        return Response::json($data, '404');
    } else {
        //Return view
        if (Request::is('admin/*')) {
            return Response::view('admin.errors.404', array(), 404);
        } else {
            return Response::view('errors.404', array(), 404);
        }
    }
}

【问题讨论】:

  • 你试过了吗,可能它也适合5.1
  • 我找不到 app/start/global.php 文件

标签: php laravel http-status-code-404 laravel-5.1


【解决方案1】:

您可以在App\Exceptions\Handlerrender 方法中为app\Exceptions\Handler.php 执行逻辑

public function render($request, Exception $e)
{
    //Handle Route Not Found
    if ($e instanceof Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
    {
        //Ajax Requests
        if($request->ajax() || $request->wantsJson())
        {
            $data = [
                'error'   => true,
                'message' => 'The route is not defined',
            ];
            return Response::json($data, '404');
        }
        else
        {
            //Return view 
            return response()->view('404');
        }
    }
    //Handle HTTP Method Not Allowed
    if ($e instanceof Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException)
    {
        //Ajax Requests
        if($request->ajax() || $request->wantsJson())
        {
            $data = [
                'error'   => true,
                'message' => 'The http method not allowed',
            ];
            return Response::json($data, '404');
        }
        else
        {
            //Return view 
            return response()->view('404'); 
        }
    }
    return parent::render($request, $e);
}

【讨论】:

    【解决方案2】:

    Laravel 可以轻松显示各种 HTTP 状态代码的自定义错误页面。例如,如果您希望自定义 404 HTTP 状态代码的错误页面,请创建 resources/views/errors/404.blade.php。此文件将针对您的应用程序生成的所有 404 错误提供。此目录中的视图的命名应与它们对应的 HTTP 状态代码相匹配。

    【讨论】:

      【解决方案3】:

      检查后备路由,您可以定义一个路由,当没有其他路由匹配传入请求时将执行该路由,后备路由应该始终是您的应用程序注册的最后一个路由。 https://laravel.com/docs/5.8/routing#fallback-routes

      Route::fallback(function () {
          //Send to 404 or whatever here.
      });
      

      【讨论】:

        猜你喜欢
        • 2016-01-12
        • 2017-05-15
        • 2016-01-30
        • 2019-08-25
        • 1970-01-01
        • 2018-10-04
        • 2016-11-24
        • 1970-01-01
        相关资源
        最近更新 更多