Load header and footer from Module in Laravel

本秂侑毒 提交于 2019-12-11 06:48:13

问题


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.


回答1:


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




回答2:


Ok i was abble to get the Auth work on Module structure, just need to add this on your routes.php

Route::group(['middleware' => ['web']], function () {
  //Your routes
});


来源:https://stackoverflow.com/questions/43097804/load-header-and-footer-from-module-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!