routes.php:

Route::get('/', 'MasterController@index'); Route::get('/', function () { return view('header'); }); Route::get('/', function () { return view('footer'); }); 

MasterController.php:

 class MasterController extends Controller { public function index() { return view('master'); } 

header.blade.php:

 @extends('master') @section('header') <h1> HEADER</h1> @endsection 

master.blade.php:

 <body> <div id="header"> <h1> @yield('header')</h1> </div> <div id="footer"> @yield('footer') </div> </body> 

footer.php:

  @extends('master') @section('footer') <h1>FOOTER</h1> @endsection 

the result should be two inscriptions "HEADER" and "FOOTER" ... so here it displays only "FOOTER" ... WHY so? In the routs they are equally spelled out then ...

    1 answer 1

    You did not quite correctly understand the meaning of @yield and confused it with @include

    @yield is a dynamic insertion so to speak, it will be filled only when section ('footer'), section ('header') are described in the called template

    @include - let's say a static insert, with @include you can split the footer, header, menu, for ease of editing

    If you want to create a template, it will look like this:

    layout.blade.php

     <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> @include('header') <div id="content"> @yield('content') </div> @include('footer') </body> </html> 

    header.blade.php

     <header> <div class="banner"></div> <h1>@yield('title') </h1> </header> 

    footer.blade.php

      <footer> Здесь футер </footer> 

    And for example page: page.blade.php

     @extends('layout') @section('title') Заголовок который пойдет в H1 @endsection @section('content') Основной текст страницы @endsection 

    B now in the controller call the page template

     public function index() { return view('page'); } 

    give you footer, header and content

    • You wrote "content" in the layout.php file, and in page.php you wrote "context", I changed it and it works! - Alexander
    • Aha .... thanks a lot, now I understand my global mistake !!!))) - Alexander
    • @ Alexander aga corrected :) - Orange_shadow