In Laravel, a master layout serves as a template that defines the overall structure of your application's views. It typically includes common elements such as the HTML structure, navigation bars, headers, footers, and any other sections that are consistent across multiple pages of your application.
You can create a master layout file in the resources/views/layouts
directory of your Laravel project. For example, let's name it app.blade.php
.
<!-- resources/views/layouts/app.blade.php -->
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>@yield('title', 'Your App')</title>
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
<header>
<!-- Navigation bar or header content -->
</header>
<div class="container">
@yield('content')
</div>
<footer>
<!-- Footer content -->
</footer>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}"></script>
</body>
</html>
To use the master layout, you need to extend it in your views and specify the content for specific sections. Use the @extends
directive to inherit the master layout and @section
and @endsection
directives to define content sections.
For example, in your view file (e.g., resources/views/home.blade.php
), you can extend the master layout and define the content section:
<!-- resources/views/home.blade.php -->
@extends('layouts.app')
@section('title', 'Home Page')
@section('content')
<div class="jumbotron">
<h1>Welcome to Your Application</h1>
<p>This is the home page content.</p>
</div>
@endsection
Note - using a master layout and partial views, you can maintain a consistent structure across your Laravel application and easily manage common elements such as headers, footers, and navigation bars.