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

Hardstyle's avatar

Pass variable to partial from base controller (variable must be global for all pages)

HI. I have a layout blade, and i`m including there head blade (portial). In the head i have a {{$title}} variable. I made a BaseController too, here i have a $title. So my question, how i can pass a title that it will be global, on all pages. And i want, that i can override it in specific page if i need

0 likes
5 replies
Hardstyle's avatar

Ok, its good to put it in the service. But if i want to rewrite it in some specific view, i just passing this variable with new value to the view in specific controller?

jekinney's avatar

Why would you "re-write it" when the data is already available?

Look up blades @inject() method. Might suit your needs. Or @yield the title in your layout with a default and use a section in a blade file to set your title.

Snapey's avatar
Snapey
Best Answer
Level 122

Assuming that in your AppServiceProvider you include;

    public function boot()
    {
        view()->share('siteTitle', 'The best new SaaS in the world');
    }

and then in your master blade layout you have;

    <title>{{ $siteTitle }}</title>

Then in your controller, you can override this by either including this line;

   view()->share('siteTitle', 'My new site title');

or pass it as a variable to the view, overriding what was previously shared

        $siteTitle = 'My new site title';
        return view('guest.talks')->with(compact('talks','region','siteTitle'));
2 likes
Snapey's avatar

I'm going to give you an additional, simpler answer;

in your master layout;

    <title>{{ $siteTitle or 'My default site title'}}</title>

Here the default will be used if $siteTitle has not been passed to the view

1 like

Please or to participate in this conversation.