【问题标题】:How to pass variables to http error layouts in Laravel?如何将变量传递给 Laravel 中的 http 错误布局?
【发布时间】:2017-01-06 03:35:24
【问题描述】:

我正在疯狂地寻找解决方案。我需要做一件非常简单的事情 - 将变量传递给自定义 404 页面 布局。 Laravel 让您可以通过创建像 /resources/views/errors/404.blade.php 这样的文件轻松地为您的 http 错误创建自定义视图,但是为什么它不能轻松地让您将变量传递给它呢?

我尝试覆盖app/Exceptions/Handler.php渲染方法:

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);
    }

但无论出于何种原因,这不起作用,我无法访问主布局中的类别变量。我正在开发一个在错误页面上显示标题的网站,但如果我无法将变量传递给错误视图,则无法创建我的标题。

未定义变量:类别

有人知道可能出了什么问题吗?这样做是不可能的吗?我读过你可以传递异常并从中获取消息,但这有什么意义呢?我不想复制整个布局并重写所有变量。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    我正在创建自定义错误布局(views/error/404.blade.php),(必须传递变量来检查用户是否登录)。 你可以使用这样的东西

    @include('layouts.article', ['mainTitle' => "404, page not found", 'mainContent' => "sorry, but the requested page does not exist :("])
    

    或者像这样

    <?php $data=[
            'mainTitle' => "404, page not found",
            'mainContent' => "sorry, but the requested page does not exist :("
        ]  ?>
    @include('layouts.article', $data)
    

    参考:Laravel Blade passing variable with string through @include causes error

    【讨论】:

      【解决方案2】:

      您正在寻找 View Composer:https://laravel.com/docs/8.x/views#view-composers

      【讨论】:

        【解决方案3】:

        添加

        public function boot()
        {
          view()->composer('my.view', function($view)
          {
            $view->with('myVariable', 'whatever you want');
          });
        }
        

        AppServiceProvider.php

        这会将whatever you want 作为$myVariable 传递到my 文件夹中的view.blade.php。可能有错别字。

        更多信息:https://laracasts.com/series/laravel-5-fundamentals/episodes/25(代码在 9:32)

        【讨论】:

        • 这仅适用于常规视图,不适用于自定义错误页面。
        【解决方案4】:

        5.4 中有一些重大更改,您应该使用:: 作为错误视图的分隔符,例如:

        View::composer('errors::*', function($view){
            $view->with('varName', 'value');
        })
        

        【讨论】:

        • 但是这在通过view('404')手动渲染视图时不起作用,所以你应该使用这个:View::composer(['errors::*', 'errors.*'], function ($view) {
        【解决方案5】:

        这个技巧对我有用。

        在你的控制器方法中:

        abort(404,json_encode($categories));
        

        在你的 404.blade.php 中:

        @php $categories = json_decode($exception->getMessage()) @endphp
        
        @foreach($categories as $category)...
        

        【讨论】:

          猜你喜欢
          • 2014-05-22
          • 1970-01-01
          • 1970-01-01
          • 2016-11-20
          • 1970-01-01
          • 2013-04-13
          • 2011-06-19
          • 2023-03-03
          • 2016-12-17
          相关资源
          最近更新 更多