如何在laravel 4.2上每次加载网站时执行php代码


how to execute php code on every time load website on laravel 4.2?

我在头上有动态菜单,我从数据库中获取其内容,所以我想知道WHERE&如何实现代码,以便在整个web应用程序上定义数据表单数据库?而不需要将代码放在负责负载视图的每个类上。

$categuries_list = DB::table('categories')
                            ->where('parent', '=', 2)
                            ->lists('name', 'id');

我想缓存我需要的东西,但在这种情况下,我会把代码放在每个类加载视图上(所以这是一个糟糕的解决方案)。

这正是视图编辑器的用途。

View::composer('header', function($view)
{
    $categuries_list = DB::table('categories')
        ->where('parent', '=', 2)
        ->lists('name', 'id');
    $view->with('categuries_list', $categuries_list);
});

只需将header与包含标头的视图的实际名称交换即可。

此代码可以放在任何正在自动加载的位置。

可以在文档中阅读有关视图作曲家的信息。http://laravel.com/docs/4.2/responses#view-作曲家