gpt4 book ai didi

Laravel:如何将变量传递给我的基本布局

转载 作者:行者123 更新时间:2023-12-05 00:21:35 25 4
gpt4 key购买 nike

我正在尝试将变量传递给我的基本布局。这是因为我在所有页面中都需要它。
我认为在我的 BaseController 上写类似的东西

protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$footertext = MyText::where('status', 1);
$this->layout = View::make($this->layout, ['footertext' =>$footertext ]);
}
}
}

我认为在我的 main.blade.php 上写类似的东西是可行的。
 {{ $footertext }}.

相反,我有这个错误,

undefined variable :页脚文本

经过两个小时的环顾……我没有找到任何解决方案。
欢迎任何帮助。

最佳答案

不久前,我也在尝试做同样的事情。

如果您使用的是 Laravel 5,您可以在 app/Providers 中编辑 AppServiceProvider.php并为此布局注册一个提供者,例如:

public function boot()
{
view()->composer('my.layout', function($view) {
$myvar = 'test';
$view->with('data', array('myvar' => $myvar));
});
}

现在,如果您使用的是 Laravel 4,我认为它更简单。在 app/filters.php :
View::composer('my.layout', function ($view) {
$view->with('variable', $variable);
});

在这两种方式中,您传递的任何变量都可用于扩展主模板的所有模板。

引用:

https://laracasts.com/discuss/channels/general-discussion/laravel-5-pass-variables-to-master-template
https://coderwall.com/p/kqxdug/share-a-variable-across-views-in-laravel?p=1&q=author%3Aeuantor

关于Laravel:如何将变量传递给我的基本布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31274875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com