Laravel

Laravel before and after middleware with example

Share your learning

Today we are going to understand laravel before and after middleware with example. 

Let’s dive into it.

So, we know that laravel has middlewares which help us to filter or modify the https requests before they actually interact with the internal application features.

We are not going to discuss how we can generate the middlewares and register them to app\http\kernel.php. Then it will be available to use on routes, route groups or in the controllers. That is already given in the laravel documentation.

Before and after middleware example

Public function handle ($request, Closure $next)
{
    // perform some operation before the closure call
    $response = $next($request);
    // perform some operation after the closure call
    Return $response;
}

As we can see in the code above, it is that simple. 

Before middleware means to perform some operations before processing the request. For example, add new data to the request or may be convert comma separated string to array etc. And after middleware means to perform the operations after processing the request and before returning the response. Then we can also manipulate or add new data to the response .

That’s it, laravel before and after middleware with example code.

Read also : PHP testing frameworks

Hope you enjoy the article, if so please feel free to share your thoughts about it in the comment section below.

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago