Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alex.lara's avatar

php data in blade templates - best practices

[laravel newbie question] I have a static class that contains pages meta info (title, description, menu links/titles, etc, etc). It doesn't have to be static but this is how it is now.

Assume I have a top bar menu, which is the same for all pages, except may be one submenu, which depends on User.

The question is: what's the right way to push pages meta info to the topbar.blade.php?

  1. I could just include a php class in blade template and then call it's functions
include_once('../../pagebase.inc')
<li><?=PageMetaFactory::link(PageMetaFactory::HOME_PAGE)?></li>
  1. It possible to share this data via view()->share(). However, that means that I have to make it an array. Furthermore, I need this meta data in controllers. So it appears that the best place to store meta data is a base Controller class. But then in order to initialize it in AppServiceProvider I will have to bring Controller dependency into it, which doesn't look right.

  2. I could pass meta info as view parameter and then access it in the template but if I have 100 page controllers then each and every will have to do the same thing again - pass the same parameter.

As you may see, I am very confused. What is the best method to address the pages meta data issue? Thank you!

0 likes
4 replies
Hameed's avatar
Hameed
Best Answer
Level 14

Hi, You may refer to the view composers documentation explained in the below link to get an idea of how to achieve your requirements

http://laravel.com/docs/5.1/views#view-composers

Please feel free to ask if you still need any further clarifications

Regards

1 like
alex.lara's avatar

Thank you for the answer!

It indeed looks like a solution, something like passing a parameter to all views in a single place.

There are only a few minor issues with that:

a) The object that is passed by ViewComposer cannot be static.

b) One has to maintain ViewComposer to ensure that all needed views are added. This somewhat duplicates what blade templates should do, because templates already define where the topbar has to be.

c) In blade templates an expression $pageMetaInfo->... will not be autocompleted (that's probably more IDE issue though)

In general, how would one build a static topbar menu in laravel? I assume top bar includes a menu, that contains links to most of the website pages.

Erik's avatar

Referring to your point b.

view()->composer(
    '*', 'App\Http\ViewComposers\GlobalComposer'
 );

When using the asterisk it will be available in every view.

1 like
alex.lara's avatar

Thank you for the remark, Erik. However, even for a simple project this may be unnecessary. After watching https://laracasts.com/lessons/view-composers I think view composers are indeed the solution.

b) in that case is addressed by moving topbar into a separate template.

P.S. really impressed by the cast quality: one of the best I have ever seen: laconic, precise, fast and complete.

Please or to participate in this conversation.