In Laravel, it is easy to break down the layouts into different sub layouts. It is a good approach to have a sub layout for a large view. Laravel supports the master layout concept. The master layout works as a placeholder where you can load different sub layouts. Also, you can pass data from different views to this view. But here I am going to talk about passing data from controller to master layout in Laravel. Generally, we redirect or load the blade view inside the controller containing the master layout. We never include the master layout inside the controller. We create a blade template by extending the master layout. Hence, for passing the data, we use that extended blade view. If you have no idea about it, then in this post, you will get getting in.
Prerequisites
To start with a new Laravel 8 project, you must have the below requirements.
- PHP >= 7.3
- MySQL (version > 5)
- Apache/Nginx Server
- Composer
Create a Project in Laravel 8
To create the new project in Laravel 8, I will use composer. Therefore, in the terminal, I have entered the below command.
composer create-project --prefer-dist laravel/laravel blog
Here, the installation has been started. After finishing the installation, just open it to the editor.
Here, the Laravel 8 application is running. Now, we will move to the next step.
Here, I am not going to use any database operation. Therefore, I will not be connecting the application with the database.
In the next step, we will create the views.
Redirect HTTP to HTTPS Using Middleware in Laravel 8
Create Master Layout in Laravel 8
First, we will create the master layout that will contain a basic bootstrap component for a header and footer. Just, navigate to the resources folder and create a new folder named layouts. Inside this layouts folder, create a blade file named master.blade.php.
<!doctype html>
<html lang="en">
<head>
<title>Programming Fields</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
{{-- Navbar --}}
<nav class="navbar navbar-expand-lg navbar-light bg-light border-bottom">
<div class="container">
<a class="navbar-brand" href="#">Programming Fields </a>
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarContent">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home </a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
</div>
</nav>
{{-- Content section --}}
@yield('content')
{{-- Footer --}}
<footer class="footer bg-light py-4 text-dark text-center fixed-bottom border-top">
<p>Copyright © 2021, Programming Fields </p>
</footer>
</body>
</html>
To test the above snippet, you will require a route.
Create REST API in Laravel 8 Using JWT Authentication
Create Route
So, for creating the route, go to the web.php file and create a new route. Here, I loaded the view directly without any controller and closure function.
Route::view('/', 'layouts.master');
Check the browser for the expected result.
Here, mater layout is ready. Now, we want to extend this master layout to another view.
So, for extending this layout, you will require to have another blade view. Therefore, just create a new blade file.
How to Create Github Login in Laravel 8 Using Socialite
Create View To Extend Master Layout
Here, I will create a view with the name homepage.blade.php. Then, I will extend the master layout inside this blade file. So, let’s do that.
@extends('layouts.master')
@section('content')
<div class="container py-5 text-center">
<h1> This is homepage content </h1>
</div>
@endsection
Now, you will have to change the route to load this view.
Route::view('/', 'homepage');
Now, navigate back to the browser and refresh the page. Here, the homepage blade content is loaded.
Now, the actual point is here. You can pass the data easily to the homepage.blade.php file using the compact method or using an array. But, how you will pass the data to its parent layout that is the master layout.
If you will try to load the master layout only then you won’t have the homepage content. If you load the homepage then it is extending the master layout. So, you cannot pass the data in the same way to the master layout.
So, in order to do that we will use the service provider class.
How to Create Login with Twitter in Laravel 8 Using Socialite
Create a Service Provider in Laravel 8
The service provider is used for bootstrapping the Laravel framework core. It registers the service container and service container bindings, event listeners, middleware, and even routes.
The service providers have register() and boot() methods.
- The register() method is used to bind functionality to the service container. You can register all custom services inside this method.
- The boot() method calls after all the service providers has been registered. So, you can say firstly, the service will be registered and then you can use it inside the boot() method.
So, create the service provider using the below command.
php artisan make:provider ContentServiceProvider
Create LinkedIn Login in Laravel 8 Using Socialite
Register Service Provider
After creating the service provider, you will need to register it. All the provider classes are registered inside the config/app.php file.
'providers' => [
...
...
...
App\Providers\ContentServiceProvider::class
];
Now, it’s time to pass the data to the Laravel master layout.
Create Socialite Login with Google Account in Laravel 8
Pass Data to Laravel Master Layout
So, for passing the data to the master layout, we will be using the boot method. Laravel provides view()->composer() function. This is a closure or class method that is used to pass the data to the view. It gets executed when a view is rendered inside the Laravel. In other words, when Laravel loads a *.blade.php file to serve to the browser for displaying any contents, it executes View Composers.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ContentServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public $menuItems;
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$this->menuItems = ["Home", "About Us", "Contact"];
view()->composer('layouts.master', function($view) {
$view->with(['contents' => $this->menuItems]);
});
}
}
Now, switch to the master.blade.php file and dump the array passed through the view()->composer().
<!doctype html>
<html lang="en">
<head>
<title>Programming Fields</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
// dump array
@dd($contents)
{{-- Navbar --}}
<nav class="navbar navbar-expand-lg navbar-light bg-light border-bottom">
<div class="container">
<a class="navbar-brand" href="#">Programming Fields </a>
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarContent">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home </a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
</div>
</nav>
{{-- Content section --}}
@yield('content')
{{-- Footer --}}
<footer class="footer bg-light py-4 text-dark text-center fixed-bottom border-top">
<p>Copyright © 2021, Programming Fields </p>
</footer>
</body>
</html>
Now, go back to the browser and refresh the page. In the response, you will be having the data passed through the view()->composer().
How to Create Facebook Login in Laravel 8 Using Socialite
Set Data to Menu Items
Now, let’s place the array items in the header navigation. So, just open the master.blade.php file and iterate the array inside the menu items.
<!doctype html>
<html lang="en">
<head>
<title>Programming Fields</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
{{-- Navbar --}}
<nav class="navbar navbar-expand-lg navbar-light bg-light border-bottom">
<div class="container">
<a class="navbar-brand" href="#">Programming Fields </a>
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarContent">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarContent">
<ul class="navbar-nav ml-auto">
<!-- display menu items -->
@foreach ($contents as $content)
<li class="nav-item active">
<a class="nav-link" href="#">{{$content}} </a>
</li>
@endforeach
</ul>
</div>
</div>
</nav>
{{-- Content section --}}
@yield('content')
{{-- Footer --}}
<footer class="footer bg-light py-4 text-dark text-center fixed-bottom border-top">
<p>Copyright © 2021, Programming Fields </p>
</footer>
</body>
</html>
Refresh the browser and see the result.
As a result, you have the dynamic data to the menu items which are passed through the service provider. You can even execute the SQL query inside the boot() method of the service provider. Never try to execute the DB operations inside the register() method. It will work only inside the boot() method.
How to Create Custom Password Reset Link in Laravel 8
Summary
We used the view()->composer() to pass the data to the Laravel master layout. These data can be accessible to any view inside the project. Generally, we don’t have the option to pass data from the controller to the master layout. Just because we extend the master layout to other blade templates. So, during the rendering of the view, we never render the master layout individually. Instead, we render the child view to render the master layout inside it automatically.
Leave a Reply