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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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.

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

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

What does laravel middleware mean? What does laravel middleware mean? Apr 09, 2024 pm 05:15 PM

Laravel middleware is an interceptor component in HTTP request and response processing, used to extend application functionality with custom logic. Middleware validates requests, modifies data, performs application operations, redirects requests, and handles errors and exceptions. Laravel provides built-in middleware such as Auth and Throttle, or you can create custom middleware. Middleware can be used through global middleware or routing middleware to enhance the security, functionality, and maintainability of your application.

Laravel middleware: controlling application access and role management Laravel middleware: controlling application access and role management Jul 28, 2023 pm 01:12 PM

Laravel middleware: controlling application access rights and role management Introduction: When developing web applications, it is often necessary to control user access rights to ensure the security of the application. The middleware in Laravel provides a concise and flexible way to manage application permissions and roles. This article will introduce how to use Laravel middleware to control application access permissions and role management. 1. What is middleware? After having an in-depth understanding of how Laravel middleware manages permissions and roles,

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

See all articles