Special Laravel Blade Pattern Code

We are currently using the Laravel framework for several projects, but one problem that we continue to work on, which I don't like, is the following problem:

Let's say you have a homepage and content page

Homepage The controller has all the PHP code on the main page. The ContentpageController has all the content specific php codes

we have app.blade.php which makes

@yield('page') 

Homepage The Controller calls up the view.blade.php homepage containing

 @extends('app') @section('page') Some HTML part @include('parts.top_5') @endsection 

ContentController calls the content.blade.php view containing

 @extends('app') @section('page') Some different HTML part @include('parts.top_5') @endsection 

Here you can see that both pages include part.top_5, the top 5 needs some specific variables to display top5. Now the problem is that we are currently copying the code for the top5 variables in both the controllers and the group middleware, but is there a better solution for generating some specific click variables when this part is turned on? So a bit like starting a controller function when loading a blade template?

I searched the internet but didn't seem to find anyone with the same question. Hope someone can help me with this mental problem!

+6
source share
1 answer

You can add this binding to AppServiceProvider

(or any custom ServiceProvider you want)

like this:

 public function boot() { $view->composer('parts.top_5', function($view) { $view->with('any_data' => 'You want'); }) } 

That way, anytime Laravel composes parts.top_5 , this closure method will parts.top_5 .

And in the docs it is here: http://laravel.com/docs/5.0/views#view-composers

+2
source

Source: https://habr.com/ru/post/987736/


All Articles