我使用Laravel 5作为我的新项目,我希望遵循最佳实践。我的布局包装为resources/views/app.blade.php
,它产生了'每个观点。我需要在app.blade.php中显示一些动态生成的数据,我应该在哪里放置逻辑来生成数据?对于任何其他视图,我会在相应的控制器中进行,但对于整个应用程序?
由于
答案 0 :(得分: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');