If laravel operates through the parent class layer, the parameters are passed to the Blade page layout.
阿神
阿神 2017-05-16 16:56:01
0
3
415

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!

阿神
阿神

闭关修行中......

reply all(3)
迷茫

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

巴扎黑
phpView::share('name', $var)
PHPzhong

The methods mentioned above are not reliable, and you cannot get the current url parameters and pass them into your function

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template