Note: this answer was written for Laravel 3 and might or might not work with the most recent Laravel 4
我最喜欢的创建动态菜单的方法是通过将菜单部分与主布局分开并通过Laravel’s Composer注入菜单数据来实现的(不要将它与Composer PHP包管理器混淆,它们是不同的)
@foreach($menuitems as $menuitem)
{{ $menuitem->title }}@endforeach
最后,我们可以通过作曲家注入变量.
// application/routes.PHP
View::composer('parts.menu',function($view){
$view->with('menuitems',Menu::all());
});
这样一来,每个部件/ menu.blade.PHP都被调用,Composer将拦截视图并注入$menuitems变量.与在return上使用一样View :: make(‘blahblah’) – >与(‘menuitems’,Menu :: all())
希望它有帮助:)
编辑:如果你不喜欢在routes.PHP中有逻辑,你可以把它放在start.PHP中,并考虑Jason Lewis将start.PHP分割成单独的文件.
在应用程序中创建一个名为start的目录,并用一些文件填充它.
+ application [DIR]
\-> + start [DIR]
|-> autoloading.PHP
|-> composers.PHP
|-> filters.PHP
\-> validation.PHP
然后将这些代码行添加到应用程序/ start.PHP的末尾
require __DIR__ . DS . 'start' . DS . 'autoloading.PHP';
require __DIR__ . DS . 'start' . DS . 'filters.PHP';
require __DIR__ . DS . 'start' . DS . 'composers.PHP';
require __DIR__ . DS . 'start' . DS . 'validation.PHP';
你有这个想法将作曲家功能放在composers.PHP中.