Table of Contents
Usage of laravel middleware:
Create middleware command
After executing the above command, a new middleware class CheckLogin.php will be created in the app/Http/Middleware directory.
After creation, you still need to register the middleware in app/Http/Kernel.php:
You can write verification in the newly created middleware as follows:
The next step is how to use the middleware function
Home Backend Development PHP Tutorial Use of laravel middleware

Use of laravel middleware

Jul 05, 2018 pm 03:13 PM
laravel middleware

This article mainly introduces the use of laravel middleware, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Usage of laravel middleware:

Create middleware command
php artisan make:middleware CheckLogin
Copy after login
After executing the above command, a new middleware class CheckLogin.php will be created in the app/Http/Middleware directory.
After creation, you still need to register the middleware in app/Http/Kernel.php:
 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,
        //这就是新注册的中间件
        'checklogin' => \App\Http\Middleware\CheckLogin::class,    ];
Copy after login
You can write verification in the newly created middleware as follows:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Session;
class CheckLogin{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $userid = Session::get(&#39;_userid&#39;);        
        $login_sts = Session::get(&#39;_login_sts&#39;);        
        if (empty($userid) || empty($login_sts)){            
        return response()->view(&#39;admin/login&#39;);
        }        
        return $next($request);
    }
}
Copy after login
The next step is how to use the middleware function
Route::group([&#39;namespace&#39;=>&#39;Admin&#39;,&#39;middleware&#39;=>&#39;checklogin&#39;],function (){    
Route::get(&#39;admins&#39;,&#39;IndexController@index&#39;);    
Route::get(&#39;logout&#39;,&#39;IndexController@logout&#39;);});
Copy after login

The routing group is used directly here. As long as the routing is placed in the group, it will go through this verification. ['namespace'=>'Admin'] is Namespace, ['middleware'=>'checklogin'] This is middleware verification. The registration name was checklogin when registering before, so just write checklogin directly after middleware.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Laravel Modify the default log file name and location

Use laravel dingo api plug-in library to create api method

The above is the detailed content of Use of laravel middleware. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to handle exceptions using middleware in Laravel How to handle exceptions using middleware in Laravel Nov 04, 2023 pm 02:26 PM

How to use middleware to handle exceptions in Laravel Middleware is an important concept in the Laravel framework. It can perform a series of operations before and after the request reaches the controller. In addition to common permission verification, logging and other functions, middleware can also be used to handle exceptions. In this article, we will explore how to use middleware to handle exceptions in Laravel and provide specific code examples. First, we need to create an exception handling middleware. You can generate a middleware class by running the following command:

How to use middleware for data export in Laravel How to use middleware for data export in Laravel Nov 02, 2023 am 08:29 AM

Laravel is a popular PHP web application framework that provides many convenient features to develop high-performance, scalable and easy-to-maintain web applications. One of the important features is middleware, which can perform certain operations between requests and responses. In this article, we will discuss how to export data to Excel files using middleware. Creating a Laravel Application First, we need to create a Laravel application. You can use co

What is laravel middleware used for? What is laravel middleware used for? Apr 09, 2024 pm 05:03 PM

Laravel middleware is used for: 1. Authentication and authorization; 2. Processing HTTP requests and responses; 3. Filtering responses; 4. Logging and monitoring; 5. Customizing application behavior. Middleware allows developers to easily add functionality and constraints to applications outside of route controllers.

How to use middleware for WeChat login authorization in Laravel How to use middleware for WeChat login authorization in Laravel Nov 03, 2023 am 10:55 AM

How to use middleware for WeChat login authorization in Laravel With the rapid development of the mobile Internet, third-party login has become a popular way for users to quickly register and log in. Among them, WeChat login is one of the most popular. For developers, how to use WeChat login for authorization in their own websites or applications is a common need. This article will introduce how to use middleware in the Laravel framework to implement the WeChat login authorization function, and provide specific code examples. First, we need to download and install Larav

Laravel middleware: Add database migration and version management to your application Laravel middleware: Add database migration and version management to your application Aug 02, 2023 am 10:17 AM

Laravel Middleware: Adding Database Migration and Version Management to Applications When developing and maintaining a web application, database migration and version management is a very important task. They allow us to easily manage the structure and data of the database without having to manually update or rebuild the database. The Laravel framework provides powerful and convenient database migration and version management functions. By using middleware, we can more easily integrate these functions into our applications. First we need to make sure our Lar

Laravel middleware: optimize database query and connection management Laravel middleware: optimize database query and connection management Jul 28, 2023 pm 07:40 PM

Laravel middleware: Optimizing database queries and connection management Overview: Laravel is a powerful PHP framework, in which middleware is one of its core features and is used to process requests and responses. In this article, we will focus on how to use Laravel middleware to optimize database queries and connection management to improve application performance and scalability. What is middleware? In Laravel, middleware are filters that handle HTTP requests. They can be executed before or after the request reaches the application

How to use middleware to implement cross-origin resource sharing (CORS) in Laravel How to use middleware to implement cross-origin resource sharing (CORS) in Laravel Nov 02, 2023 pm 01:57 PM

Overview of how to use middleware to implement cross-origin resource sharing (CORS) in Laravel: Cross-domain resource sharing (CORS) is a browser mechanism that allows web applications to share resources under different domain names. Laravel, a popular PHP framework, provides a convenient way to handle CORS by using middleware to handle cross-domain requests. This article will introduce you how to use middleware to implement CORS in Laravel, including how to configure middleware, set allowed domain names and requests

What are laravel middlewares What are laravel middlewares Apr 09, 2024 pm 03:33 PM

Laravel middleware is divided into five types: global, routing, group, termination and custom. Global middleware applies to all requests, route middleware only applies to specific routes, group middleware applies to a set of routes, termination middleware executes after all other middleware and handlers execute, custom middleware is created by the developer and Extends the BaseMiddleware class.

See all articles