Search code examples
phplaravelviewcontrollers

Load header and footer from Module in Laravel


I have created a module in Laravel and im using views on that module, my structure is this:

Modules

 -> MyModule
 ->->Controllers
 ->->Views
 ->->->MyModule.blade.php

But i have the headers and footer done on resources->views->layouts->base.blade.php

So how can i call this one so i can use the same base layout in all modules? it is possible on Laravel 5?

Already tried this

@include('layouts.base')

but im getting

Trying to get property of non-object (View: ... resources\views\layouts\base.blade.php

Thank you.


Solution

  • The structure of blade is relative to the views folder in the the resources folder.

    Thus making your @include() have a structure like the this:

    @include('DIRECTORY.BLADE')
    

    and you can include your various blade contents by using @yield()

    @yield('YIELD_FIELD_NAME')
    

    If you are trying to have blades extent from that layout you would call that at the top of the blade files you want to extend off it.

    @extends('DIRECTORY.BLADE')
    

    This is an example blade file that can extend your layout if your layout contains the @yield('content') tag in it.

    example.blade.php

    @extends('layouts.base')
    
    @section('content')
    
      YOUR BLADES HTML/CONTENT
    
    @endsection
    

    https://laravel.com/docs/5.4/blade#defining-a-layout

    How to add Auth middleware to controller:

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }
    

    Here is an example Controller: https://github.com/jeremykenedy/laravel-auth/blob/master/app/Http/Controllers/UsersManagementController.php

    Here is an example of a view that uses that controller: https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/usersmanagement/show-user.blade.php

    Here is an example of the template that view uses: https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/layouts/app.blade.php

    Here is the routing file for the above examples: https://github.com/jeremykenedy/laravel-auth/blob/master/routes/web.php