For variables used in layout, if parameters are passed from the controller, the operation needs to be repeated for each view!
How to solve the problem of passing the result to the layout after an operation for all views to use (I don’t want business code to appear in the layout view).
eg:
1. Permission management, set different menus according to different users!
2. Dynamic numbers in the menu, such as [Today’s Order (50)], that 50 requires all controllers to obtain the data and then pass it to the view!
迷茫2017-05-16 16:58:01
There are two ways to solve it:
First: TraditionalViewComposerProvider
class NavViewComposerProvider extends ServiceProvider {
public function boot(){
view()->composer('partials.nav', function ($view) {
$view->with('userAtLayout', User::find(Auth::user()->id));
//根据自己的需求改呗
});
}
}
Second, Laravel 5.1 new features @inject:
@inject('var','Class')
// var 变量名 Class绑定的类名
For example, you can do this:
@inject('nav','App\SiteNav')
Just query the data in SiteNav.
Digression: Read the documentation carefully and you can solve 80% of the problems you encounter.
Happy Hacking
PHPz2017-05-16 16:58:01
The methods mentioned above are not reliable, and you cannot get the current url parameters and pass them into your function