Modularize only Views of Laravel

0

I have the following structure:

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

But I have a problem, I can not use the blade directives without conflict, if I use within modulo02 / view01 calling another file that is inside the same module it generates an error because it can not find the file, I have which pass the complete path to work for example:

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

But I wanted the blade to understand which folder it was, like the context, so I did not have to call the full path, is it possible?

    
asked by anonymous 21.09.2018 / 21:58

1 answer

3

Friend, I believe what you are looking for is "namespaces" in views.

I'll show you two ways (and there are probably still 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 views 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 you access the url "/ admin ", the browser should display the message " hello from admin ".

Option 2 , add the namespace dynamically:

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 views 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 you access the url " / module1 " and " / module2 ", the browser should display its "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 fires when a view is repackaged: Laravel: View Composers

You can use this view composer to dynamically set the namespace for example, it's just a matter of using creativity.

I did not try but, I think I could still add these namespaces on the routes, or even through middlewares. I've used something like that once, and on my system I've created a ViewServiceProvider and solved it there. It's a question of seeing what gets better on your system, use the same creativity.

I hope it helps.

    
26.09.2018 / 17:06