How To Prevent Back Button In Laravel 10

  Jul 2023
  Category: Laravel
How To Prevent Back Button In Laravel 10

Hi developers, In laravel application we all face the back button issue . While clicking the back button will sometime leads the user to some unwanted or outdated data that should be avoided . In this tutorial we will see how to disable back button in laravel 10.

Middleware was used to moniter and filter all the incoming HTTP requests entering in a Laravel Application. For example the middleware moniters the authenticated routes and allows only the authenticated users otherwise it will redirect to the Login page. If the user is authenticated it will allows to proceed the application.

We can also additionally create more middleware as per the user requirement which allows the developers to write more strict validation codes to the project.

Create a Custom Middleware

To create a custom middleware, run the following Artisan command.

php artisan make:middleware PreventBackButtonMiddleware

Implement the Middleware Logic

Open the PreventBackButtonMiddleware class and locate the handle method. This method is where the middleware logic resides. Modify the method as follows.


namespace App\Http\Middleware;

use Closure;

class PreventBackButtonMiddleware
    public function handle($request, Closure $next)
        $response = $next($request);
        $response->headers->set('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
        $response->headers->set('Pragma', 'no-cache');
        $response->headers->set('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT');

        return $response;
}                               php artisan make:middleware PreventBackButtonMiddleware

Register the Middleware

To make use of the newly created middleware, we need to register it in the Laravel application.

Open the app/Http/Kernel.php file and locate the $middleware property. Add the following line to register the PreventBackButtonMiddleware.


namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
     * The application's route middleware.
     * These middleware may be assigned to groups or used individually.
     * @var array
    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'prevent-back-button' => \App\Http\Middleware\PreventBackButtonMiddleware::class,

Apply the Middleware

You can apply the middleware at different levels depending on your specific requirements. you can apply it to specific routes or groups of routes by adding the middleware to the route definitions in the routes/web.php or routes/api.php files.

Route::group(['middleware' => 'prevent-back-button'],function(){
	Route::get('/home', 'HomeController@index');

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.


ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides