获取404页

时间:2016-06-20 12:25:47

标签: laravel laravel-5 httprequest

嘿我想在自定义404刀片文件中使用当前请求对象作为外观而不是静态方式($request而不是Request::)。

我不知道我是否可以向错误处理程序提示它,或者是否有办法创建该对象?

我应该/可以通过Expections/Handler.php文件吗?

我发现Here以下答案:

//Create a view and set this code in app/Exception/Handler.php :

/**
 * Render an exception into a response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if($e instanceof NotFoundHttpException)
    {
        return response()->view('missing', [], 404);
    }
    return parent::render($request, $e);
}

 //Set this use to get it working :

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

这是正确的方法吗?

1 个答案:

答案 0 :(得分:1)

是的,您可以通过处理程序执行此操作。在 render()方法内:

if ($e instanceof NotFoundHttpException) {
    return response()->view('your.view.name', $dataYouWantToPass);
}
相关问题