laravel5.2 访问一个不存在的路由时,如何跳转到404,lnmp环境,要怎么配置?
                                                                                                                                                                                                 回复内容:                                                                                  laravel5.2 访问一个不存在的路由时,如何跳转到404,lnmp环境,要怎么配置?
理论上你把 debug 关了,线上环境是会自动到 404 的。
你是想要「跳转到 404 页」还是「显示 404 页」?如果是要跳转的话,请配置 app/exceptions/handler.php,并在 notfoundexception 被抛出时返回一个 redirect 响应。
1、如果你只是想抛出404错误,debug开关可以满足你;2、如果你想处理异常或自定义异常,可参照如下;在laravel项目根目录下的app下的exceptions目录下的handler.php文件;我们可以在这里自定义异常以及处理异常;
最常见的莫过于modelnotfoundexception
下面是一个demo:
route:vikin.cc/article/8
handler file://处理http响应异常public function render($request, exception $e){    switch($e){                //使用类型运算符 instanceof 判断异常(实例)是否为 modelnotfoundexception        case ($e instanceof modelnotfoundexception):            //进行异常处理            return $this->renderexception($e);            break;        default:            return parent::render($request, $e);    }}//处理异常protected function renderexception($e){   switch ($e){       case ($e instanceof modelnotfoundexception):                      //自定义处理异常,此处我们返回一个404页面           return view('errors.404');           break;       default:                  //如果异常非modelnotfoundexception,我们返回laravel默认的错误页面           return (new symfonydisplayer(config('app.debug')))                  ->createresponse($e);   }}
通过上述案例,你可以轻松的处理异常,并给用户一个友好的提示!
   
 
   