简体   繁体   English

为什么在Laravel中出现“未定义的变量:var”错误?

[英]Why am I getting and “Undefined variable: var” error in Laravel?

I've been trying to pass some data to be avaliable in my views but after following several guides I always get a Undefined variable: var error. 我一直在尝试传递一些数据以使其在视图中可用,但是在遵循了几条指南之后,我总是得到一个Undefined variable: var错误。

I've added to app/Providers/AppServiceProvider.php in 2 different attemps: 我已经在2次不同的尝试中将其添加到app/Providers/AppServiceProvider.php中:

Using share(): 使用share():

public function boot()
{
        // Make this custom global data avaliable to all views
        $var = "Foobar";
        view()->share('var', $var);
}

Using composer(): 使用composer():

public function boot()
    {
            view()->composer('partials.navbar', function($view){
                $view->with('var', 'Foobar');
            });
    }

resources/views/partials/navbar.blade.php: 资源/视图/部分/navbar.blade.php:

<ul class="nav navbar-nav">
    <li class="active"><a href="#">Home</a></li>
    <li><a href="about">About</a></li>
    <li><a href="somewhere">{{$var}}</a></li>
</ul>

I'm completely new to Laravel so any help is greatly appreciated. 我是Laravel的新手,因此非常感谢您的帮助。 Thanks! 谢谢!

Laravel pre-compiles certain files to improve performance. Laravel预编译某些文件以提高性能。 The AppServiceProvider is one of them... AppServiceProvider是其中之一...

To clear the pre-compiled classes, run php artisan clear-compiled and to pre-compile them again run php artisan optimize . 要清除预编译的类,请运行php artisan clear-compiled ,然后再次对其进行预编译,请运行php artisan optimize

To change the classes that are pre-compiled and for more information on the topic in general, take a look at this answer 要更改预编译的类以及有关该主题的一般信息,请查看此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM