当前位置:网站首页>Laravel document sorting 8. Middleware
Laravel document sorting 8. Middleware
2022-06-25 04:19:00 【Angry devil】
Preface :Laravel Document sorting , Only for record , Nothing else .
1、 What is Middleware ?
See the word and know the meaning , In the middle , A component used to handle a business .http middleware , It provides a convenient mechanism to filter the data entering the application http request , Only after being filtered by middleware , The program will continue to run .
2、 Common middleware functions
Auth middleware , Verify the identity of the user
Cors middleware , Responsible for responding to all departing programs , Add appropriate headers
Log Middleware , Responsible for recording all incoming requests to the application .
3、laravel Where is the middleware of the framework ?
App/Http/Middleware
4、 How to create a middleware ?
php artisan make:middleware OldMiddleware
Ps: It can be created manually , It can also be done through artisan Command to generate .
This command will be in app/Http/Middleware Set a name in the directory as OldMiddleware Class
5、OldMiddleware Middleware instance
<?php
namespace App\Http\Middleware;
use Closure;
class OldMiddleware
{
/**
* Run the request filter .
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->input('age') <= 200) {
return redirect('home');
}
return $next($request);
}
}
Age Less than 200, The middleware will return http Request relocation to client , otherwise , Progress incoming applications .
6、 If you wish http request , Through your middleware , What to do with it ?
Add middleware to app/Http/kernel.php Of documents $middleware Properties of the .
7、 If you want to specify middleware for a route separately , How to deal with it ?
a、 stay app/Http/Kernel.php Of documents $routeMiddleware Add a set of customized key values of the middleware to the attribute , as follows :
// stay App\Http\Kernel Within class ...
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
];
b、 In routing file , Use the added middleware key name for routing , that will do .
middleware , Once in a kernel The document specifies , In the routing options , Use middleware Key value assignment :
Route::get('admin/profile', ['middleware' => 'auth', function () {
//
}]);
c、 You can also use multiple middleware for a route , as follows :
Route::get('/', ['middleware' => ['first', 'second'], function () {
//
}]);
d、 except c This method , You can also chain call middleware Method
Route::get('/', function () {
//
}])->middleware(['first', 'second']);
8、 Middleware parameters
Before running an operation , To verify whether the current user has this 【 role 】, You can create a role middleware , Receive role name , As an additional parameter :
<?php
namespace App\Http\Middleware;
use Closure;
class RoleMiddleware
{
/**
* Run request filtering
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $role
* @return mixed
*/
public function handle($request, Closure $next, $role)
{
if (! $request->user()->hasRole($role)) {
// If the user has no specific 「 role 」 Words
}
return $next($request);
}
}
In routing , Colons can be used : To interval the middleware name and assignment parameters , Multiple parameters can be separated by commas
Route::put('post/{id}', ['middleware' => 'role:editor', function ($id) {
//
}]);
9、Terminable middleware
This middleware , It's to satisfy http After the request is sent to the browser , To deal with session Storage operation .
<?php
namespace Illuminate\Session\Middleware;
use Closure;
class StartSession
{
public function handle($request, Closure $next)
{
return $next($request);
}
public function terminate($request, $response)
{
// preservation session data ...
}
}
Once the middleware is defined , You need to add it to kernel In the global middleware list of the file .
When you call terminate() When the method is used ,laravel A new middleware instance will be parsed from the server container .
If you wish handle() terminate() Methods are invoked using consistent middleware , that , You need to use containers singleton Method register middleware .
边栏推荐
- SEO的5大关键指标:排名+流量+会话+停留时长+跳出率
- BSC parsing input data of transaction
- Standing wave ratio calculation method
- cesium 图形标注圆形、正方形、多边形、椭圆等
- 1. first knowledge of chromatic harmonica
- Development of trading system (XII) - Official quickfix document
- 微信小程序中的列表渲染
- [team learning] SQL programming language notes - task04
- Mysql的order by
- 地方/园区产业规划之 “ 如何进行产业定位 ”
猜你喜欢

Lecture record: history and development of strapdown inertial navigation solution

“语法糖”——我的编程新知

代表多样性的彩色 NFT 系列上线 The Sandbox 市场平台

"Comment positionner l'industrie" dans la planification industrielle locale / parc

讲座记录《捷联惯导解算的历史及发展》

Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand

Hello CTP (II) -- Introduction to CTP

如何绘制产业招商地图

Lecture record: data processing methods and applications of various spatial geodetic techniques

NFT insider 63: the sandbox reached a cooperation with Time magazine, and YGG established Spain's subdao
随机推荐
Development of trading system (V) -- Introduction to Sinovel counter
数学分析_笔记_第3章:极限
Hello CTP (III) - CTP quotation API
OBS Browser+浏览器的基本使用
[harmony OS] [arkui] ETS development graphics and animation drawing
SQL, CTE, flg case problems
【LeetCode】22. 括号生成
小心被偷脸!天天用的人脸识别风险原来这么多?
Development of trading system (IX) -- dark pool technology
opencv最大能打开多少图像?
Development of trading system (XI) -- Introduction to quickfix
Trading system development (IV) - trading counter system
地方/园区产业规划之 “ 如何进行产业定位 ”
List rendering in wechat applet
Numpy NP tips: squeeze and other processing of numpy arrays
[proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube
IntStream API介绍
Error 1062 is reported during MySQL insertion, but I do not have this field.
Development of trading system (II) -- market data
讲座记录《多种空间大地测量技术的数据处理方法和应用》