【问题标题】:Undefined variable: title, in Laravel未定义的变量:标题,在 Laravel
【发布时间】:2014-09-26 18:03:00
【问题描述】:

我定义了我的方法控制器 index() 并传递参数标题以在我的 html 视图中显示:

class WebController extends BaseController{
    public $layout = 'layout.base';

    /*Index*/
    public function index(){
        $this->layout->title = 'Index Title';
        return View::make('pages.index');
    }
}

但是当我加载页面时返回此错误:

在我的 'app\views\layout\base.blade.php' 我有:

<!DOCTYPE html>
<html lang="es">
    <head>
        <title>{{$title}}</title>
        @section('metas')
        @show

        @section('styles')
        @show
    </head>

这是我的 route.php:

Route::get('/index',['as' => 'pages.index', 'uses' => 'WebController@index']);

如何解决这个问题? 非常感谢!

【问题讨论】:

标签: php laravel


【解决方案1】:

尝试写调用你的视图

return View::make('pages.index'); 

return View::make('pages.index')->with('title', $this->layout->title);

【讨论】:

    【解决方案2】:

    尝试向view::make添加一个包含数据的数组:

    return View::make('pages.index', array('title' => 'Title'));
    

    【讨论】:

    • 是的,它有效,但不存在最优雅的方法吗?
    【解决方案3】:

    试试这样,你必须在返回 View::make(..) 时传递变量。 当您想传递更多变量时,您可以使用 compact();功能。

    /*Index*/
        public function index(){
            $title = 'Index Title';
            return View::make('pages.index', $title);
        }
    

    compact() 示例;

    return View::make('pages.index', compact('title','description'));
    

    【讨论】:

      猜你喜欢
      • 2021-01-10
      • 2020-06-12
      • 2018-01-08
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      • 2015-05-15
      相关资源
      最近更新 更多