Can I have a Laravel 4.2 blade include with it's own controller?

simonw

New Member
#1
I have a sidebar include file that is present on every page of the website I am working on.

layout.blade:
Mã:
<div class="after-login buying-process-wrapper">
  <!--page content-->
  {{ $content }}
  <!-- end of page content-->

  <!-- Sidebar -->
  @include('layout.sidebar')
  <!-- End of Sidebar -->
</div>
My controller sets $layout and renders the above blade file, but layout.sidebar is an include file which requires PHP to populate it's content.

Can I set a controller/route for this sidebar alone (and how would I do it?), or am I forced to have to duplicate the same calls to the function that handles the sidebar content in every controller?

I'm trying to find a better solution than having to go in to every controller and calling ->sidebar() every time each page is loaded.

Thanks
 

Admin

Administrator
Thành viên BQT
#2
Why are you still using Laravel 4.2? It't 5.7 now and I strongly recommend you to upgrade to the latest version.

Answer to your question:

No, you don't need to call functions that handle your sidebar contents in every controller. You can share you common data across all or some of your views by using View Composer. From Laravel4.2 Documentation:
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want bound to a given view each time that view is rendered throughout your application, a view composer can organize that code into a single location. Therefore, view composers may function like "view models" or "presenters".
If you are using newer versions of Laravel, remember to switch to the right documentation from the right upper corner.

Also, when using Blade @include directive, you can optionally pass variables into the "included" components:
Mã:
@include('layout.sidebar', ['my_var' => 'value goes here'])
And you can use {{ $my_var }} in your component just like you normally would in your blade templates.
 
Top