我刚开始使用Laravel并且正在将一堆网站移植到框架中.
该网站的一个功能是在标题中动态添加图像.我正在使用一个常见的Blade模板,并且想知道是否有任何方法可以将随机变量(1到4之间的整数)注入到使用该布局的每个View中.
我想做的是能够在通用模板中添加类似的东西 –
< img src =“img / cutouts / cutout-<?= $randomInt;?> .jpg”alt =“”/>
将$randomInt发送到每个View
解决方法
你可以看看
View composers
所以你会有类似的东西:
View::composer('your.view',function($view) { $view->with('randomInt',rand(1,4)); }
每次使用’your.view'(或其他)视图时,都会传递$randomInt变量.