dsfds2343 2016-08-30 01:27
浏览 67
已采纳

如何在Laravel中将变量传递给http错误布局?

I am going crazy looking for a solution to this. I need to do a very simple thing - pass variables to the custom 404 page layout. Laravel lets you easily create a custom view for your http errors by creating a file like /resources/views/errors/404.blade.php but why doesn't it easily let you pass variables to it?

I tried overwriting app/Exceptions/Handler.php render method:

public function render($request, Exception $e)
    {
        if($this->isHttpException($e)){
            switch ($e->getStatusCode()) {
                case '404':
                      parent::render($request, $e)->flash();
                      $categories = Category::hierarchy();
                      return View::make('errors.404')->with(['categories' => $categories]);
                break;

                default:
                    return $this->renderHttpException($e);
                break;
            }
        }
        return parent::render($request, $e);
    }

But for whatever reason this doesn't work and I cannot access the categories variable in my master layout. I'm working on a site that displays header on error pages but if I can't pass variables to the error view then my header cannot be created.

Undefined variable: categories

Anyone has an idea of what could be going wrong? Is it just impossible to do this? I have read you can pass the exception in and get a message from it but what's the point of that? I don't want to have to duplicate the entire layout and rewrite all variables.

  • 写回答

2条回答 默认 最新

  • du7979 2016-08-30 01:54
    关注

    You're looking for a View Composer: https://laravel.com/docs/5.3/views#view-composers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题