Friend, I believe what you are looking for is "namespaces" in the views.
I’ll show you two ways to do it (and probably there are other ways).
Option 1:
app/Providers/Appserviceprovider.php
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->app['view']->addNamespace('admin', base_path() . '/resources/views/admin');
}
route/web.php
Route::get('/admin', function () {
return view('admin::index');
});
And having the following view structure:
resources/views/admin/index.blade.php
resources/views/admin/partials/hello.blade.php
Resources/views/admin/index.blade.php
@include('admin::partials/hello')
Resources/views/admin/partials/hello.blade.php
hello from admin
When accessing the url "/admin", the browser should display the message "hello from admin".
Option 2, dynamically add the namespace:
route/web.php
Route::get('/modulo1', function () {
app('view')->addNamespace('modulo', base_path() . '/resources/views/modulo1');
return view('modulo::index');
});
Route::get('/modulo2', function () {
app('view')->addNamespace('modulo', base_path() . '/resources/views/modulo2');
return view('modulo::index');
});
And having the following view structure:
resources/views/modulo1/index.blade.php
resources/views/modulo1/partials/hello.blade.php
resources/views/modulo2/index.blade.php
resources/views/modulo2/partials/hello.blade.php
Resources/views/modulo1/index.blade.php
@include('modulo::partials/hello')
Resources/views/modulo1/partials/hello.blade.php
hello from modulo1
Resources/views/modulo2/index.blade.php
@include('modulo::partials/hello')
Resources/views/modulo2/partials/hello.blade.php
hello from modulo2
When accessing the url "/modulo1" and "/modulo2", the browser should display its messages "hello from modulo1" and "hello from modulo2".
I created a very simple repository with the test files: example-using-Vies-namespaces-Laravel
Laravel provides a hook that is fired when a view is reinderized: Laravel: View Composers
You can use this Composer view to dynamically set the namespace for example, it’s just a matter of using creativity.
I did not test but, I believe that I could still add these namespaces in the routes, or even through middlewares. I used something like this once, and in my system I created a Viewserviceprovider and I decided there. It’s a matter of seeing what looks best in your system, use the same creativity.
I hope it helps.