未定义变量:标题,在 Laravel 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25115720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:54:00  来源:igfitidea点击:

Undefined variable: title, in Laravel

phplaravel

提问by Funny Frontend

I′m define my method controller index() and I pass param title for show in my html view:

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

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

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

But when I load the page return this error: enter image description here

但是当我加载页面时返回此错误: 在此处输入图片说明

And in my 'app\views\layout\base.blade.php' i have:

在我的“app\views\layout\base.blade.php”中,我有:

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

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

This is my route.php:

这是我的 route.php:

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

How to fix this? Thank you very much!

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

采纳答案by Sander

Try to add an array with data to the view::make:

尝试将包含数据的数组添加到view::make

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

回答by Duikboot

Try something like this, you have to pass the variable when you return the View::make(..). When you would like to pass more variables you can use the compact(); function.

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

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

Example with compact();

使用 compact() 的示例;

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

回答by Sachin

try to write call your view from

试着写下你的观点

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

to

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