Modularize only Views of Laravel

Asked

Viewed 191 times

0

I have the following structure:

- resources/
  - views
    - Modulo1
     - view01.blade.php
    - Modulo2
     - view01.blade.php

However I have a problem, I can not use the directives of Lade without conflict, if I use within modulo02/view01 calling another file that is within the same module it generates an error because it does not find the file, I have to go the whole way to work for example:

  @include('Modulo02.pasta1.view2');

But I wanted Blade to understand which folder he was in, like the context, so I didn’t have to call the full path that’s possible ?

1 answer

4


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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.