当前位置:网站首页>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 .
边栏推荐
- 冷热酸甜、想成就成?冷酸灵母公司登康口腔欲在深交所主板上市
- 长沙“求才”:“下力气”与“出实招”并进,“快发展”和“慢生活”兼得
- Hello CTP (I) - basic knowledge of futures
- web3.eth. Getpendingtransactions() returns null?
- 1、项目第二阶段——用户注册和登陆
- PHP code audit 1 - php Ini
- 练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。
- WMS仓储管理系统的使用价值,你知道多少
- 微信小程序中的列表渲染
- Flutter Builder & futurebuilder components
猜你喜欢

WMS仓储管理系统的使用价值,你知道多少
![[proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube](/img/28/33f3e9736a68439b5bcdc4e75c939c.png)
[proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube

2. play the chromatic harmonica

小心被偷脸!天天用的人脸识别风险原来这么多?

Nodejs 通过Heidisql连接mysql出现ER_BAD_DB_ERROR: Unknown database 'my_db_books'

【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示

2.吹响半音阶口琴

PHP代码审计1—PHP.ini的那些事

文本关键词提取:ansj

2D 照片变身 3D 模型,来看英伟达的 AI 新“魔法”!
随机推荐
Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand
uniapp 制作手机app程序, 使用uni.chooseVideo录制视频,视频播放模糊分辨率低的原因
Upgrade cmake
2022-06-21-flink-49 (I. SQL manual)
Nodejs 通过Heidisql连接mysql出现ER_BAD_DB_ERROR: Unknown database 'my_db_books'
MySQL order by
AI quantitative transaction (II) -- tushare financial data framework
The art of writing simple code
Serious PHP defects can lead to rce attacks on QNAP NAS devices
智慧风电:数字孪生 3D 风机智能设备运维
opencv最大能打开多少图像?
Intel 13th generation core showed its true colors for the first time: 68mb cache improved significantly
Summary of various problems encountered by cocos2d-x
Cesium 加载显示热力图
无法安装redis接口
kenlm
5 key indicators of SEO: ranking + traffic + session + length of stay + bounce rate
Mathematical analysis_ Notes_ Chapter 3: limits
[harmony OS] [arkui] ETS development graphics and animation drawing
95% 程序员都在这里摸鱼……