2015-02-09 72 views
2

我为我的新项目使用了Laravel 5,我想遵循最佳实践。我有我的布局包装为resources/views/app.blade.php,它'产生'每个视图。我需要在app.blade.php中显示一些动态生成的数据,我应该在哪里放置逻辑来生成数据?对于任何其他观点,我会在相应的控制器中做到这一点,但对于整个应用程序?Laravel,在哪里放置app.blade.php文件的逻辑?

谢谢

回答

1

你可以使用ViewComposer!它在文档中有一些示例,此链接http://laravel.com/docs/5.0/views

+0

此链接可能会回答问题,但请至少复制这里回复OP问题的部分,记住链接可能会被破坏或过时。 – 2015-02-09 17:02:06

+0

是否存在类似“整个应用程序的控制器”? – 2015-02-09 17:06:01

0
<?php namespace App\Http\Composers; 

use Illuminate\Contracts\View\View; 
use Illuminate\Users\Repository as UserRepository; 

class ProfileComposer { 

    /** 
    * The user repository implementation. 
    * 
    * @var UserRepository 
    */ 
    protected $users; 

    /** 
    * Create a new profile composer. 
    * 
    * @param UserRepository $users 
    * @return void 
    */ 
    public function __construct(UserRepository $users) 
    { 
     // Dependencies automatically resolved by service container... 
     $this->users = $users; 
    } 

    /** 
    * Bind data to the view. 
    * 
    * @param View $view 
    * @return void 
    */ 
    public function compose(View $view) 
    { 
     $view->with('count', $this->users->count()); 
    } 

} 

并绑定到视图!

View::composer(['profile', 'dashboard'], 'App\Http\ViewComposers\MyViewComposer');