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

zettz's avatar
Level 1

Multiple controller for single view

Hi, I need help with my view, my view need to contain 2 different controller, as far as I read, no such things, mostly 1 view 1 controller, but is there a way to do it? Like using @include inside main view by separating each view for Application settings and Config setting?

For example I got Configuration view, inside the view I need to use 2 controllers, like ApplicationController to manage application things, like "date, web name, web slogan" settings, and ConfigController to manage config for global use, like, "Invoice model type, invoice prefix, invoice suffix"

Thank you.

0 likes
1 reply
neilherbertuk's avatar
Level 9

Hi ZETTZ,

This can be achieved using a View Composer and a service provider.

Within a service provider, this could be the default AppServiceProvider but should probably be in it's own Service Provider.

There are a couple of ways of doing this, here is a basic example of using a closure to return something to your views. Within the boot method add:

View::composer('*', function ($view) {
  // Logic here
    $view->with('config', 'This is the config value');
});

The * acts as a wildcard, you could put a single view here, or an array list of views you want to make this available too. Using the wildcard, within your views would be a globally available variable named $config which will return the string value 'This is the config value'.

You can also use the route style syntax and include a view composer class to separate the logic into its own place.


// Using class based composers...
        View::composer(
            '*', 'App\Http\ViewComposers\ConfigComposer'
        );

You would then create a view composer class and use the same $view->with logic to add your config.

Neil

Some resources:

Please or to participate in this conversation.