laravel 拾遗 中间件
Problem
You want to add middleware to your application but don't know where to begin.
Solution
Create a simple middleware class.
Step 1 - Create the class
<?php namespace MyApp; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface; class Middleware implements HttpKernelInterface { protected $app; /**
* Constructor
*/
public function __construct(HttpKernelInterface $app)
{
$this->app = $app;
} /**
* Handle the request, return the response
*
* @implements HttpKernelInterface::handle
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param int $type
* @param bool $catch
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handle(Request $request,
$type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
// 1) Modify incoming request if needed
... // 2) Chain the app handler to get the response
$response = $this->app->handle($request, $type, $catch); // 3) Modify the response if needed
... // 4) Return the response
return $response;
}
}
?>
Step 2 - Register the Middleware Class
You need to do this in the register()
method of a service provider.
App::middleware('MyApp\Middleware');
Alternatively you can install a simple package I created which allows you to register your middleware inapp/start/preboot.php
. See Laravel-Hooks for details.
Discussion
The above class doesn't do anything.
But it's a good skeleton to start with. Obviously, you'll need to change the namespace and classname to fit your application.
Then you may want to try logging something to make sure it works. You can update the handle()
method of your class as specified below.
// In step #1) Modify incoming request if needed // Log to a file. Since app/start/global.php hasn't been hit
// yet the Log facade isn't set to log to a file yet. So just
// write directly to a file.
$logfile = storage_path().'/logs/laravel.log';
error_log("Middleware entry\n", 3, $logfile); // In step #3) Modify reponse if needed // Log to a file. We're safe to use the Log facade now that
// it should be set up in app/start/global.php
Log::info("Middleware exit");
Now you can examine your app/storage/logs/laravel.log
file to see that your middleware works.
laravel 拾遗 中间件的更多相关文章
- Laravel 使用中间件进行权限控制
Laravel 使用中间件进行权限控制 飞凡的陀螺 关注 2018.01.24 17:45 字数 264 阅读 1138评论 0喜欢 1 先看 文档Laravel 中间件提供了一种方便的机制来过滤进入 ...
- laravel StartSession中间件的实现原理
1. 打开app\Http\Kernel.php,找到StartSession的位置.这里要说一下,middleware中的中间件是都会被执行的,但执行的顺序我不知道,还需看源码来实现 protect ...
- Laravel之中间件
一.中间件的作用 HTTP 中间件提供了一个便利的机制来过滤进入应用的 HTTP 请求.例如,Laravel 包含了一个中间件来验证用户是否经过授权,如果用户没有经过授权,中间件会将用户重定向到登录页 ...
- laravel的中间件创建思路
网上有很多解析laravel中间件的实现原理,但是不知道有没有读者在读的时候不明白,作者是怎么想到要用array_reduce函数的? 本文从自己的角度出发,模拟了如果我是作者,我是怎么实现这个中间件 ...
- 深入解析Laravel的中间件
Laravel 中间件是什么? 简而言之,中间件在 laravel 中的作用就是过滤 HTTP 请求,根据不同的请求来执行不同的逻辑操作. 我们可以通过中间件实现以下功能: 指定某些路由 设置 HTT ...
- laravel 加中间件的方法 防止直接打开后台
路由 routes.php Route::group(['middleware' => ['web','admin.login.login']], function () { //后台首页路由 ...
- Laravel 5 中间件、路由群组、子域名路由、 权限控制的基本使用方法
创建控制器: php artisan make:controller Admin/IndexController 创建Middleware: php artisan make:middleware T ...
- Laravel 利用中间件控制权限。
1.把所有需要控制的url写到config/admin.php里. 2.http/kernel.php文件里加上中间件. 3.http/Middleware/Permission.php 4.视图层
- laravel在中间件内生成的变量如何传到控制器
在中间件内获取到一个变量,如何返回到控制器中并使用这个变量! 做了个demo: // web.php Route::get('/check', 'CheckController@check')-> ...
随机推荐
- Effective C++ 38-42
38.绝不要又一次定义继承而来的缺省參数值. 又一次定义函数缺省參数值意味着又一次定义函数.而非虚函数不能又一次定义,所以将就考虑不能又一次定义虚函数的缺省參数值的原因:虚函数是动态绑定的而缺省參数值 ...
- B/S与C/S的差别
前一段时间已经结束了C/S的学习,開始了B/S的旅程,那么为什么我们要学习这两个,这两个有什么差别呢?这些差别你知道多少呢? B/S结构.即Browser/Server(浏览器/server)结构.是 ...
- python学习笔记之基础数据和控制
注释: 单行注释 # 多行注释''' ''' 注意:当注释中有汉字时需要在python文件的第一行添加如下内容之一:#coding:gbk或#coding:utf-8或##-*- coding ...
- linux上传下载文件rz,sz
[一般用于SecureCRT ssh中使用] █ 法一:直接用yum安装lrzsz(推荐) yum install lrzsz -y 注意:rhel安装完系统后 直接可使用 rz sz █ 法二:源文 ...
- 小程序九:导航&地图&画布
navigator 导航 属性名 类型 默认值 说明 url String 应用内的跳转链接 redirect Boolean false 是否关闭当前页面 hover-class String ...
- ___cxa_pure_virtual", referenced from
加入百度地图之后报这种错,解决方法:将project中的.m文件改一个成为.mm文件.
- 原创:微信小程序调用【统一下单】、【支付】、【支付回调】api并处理请求
1.服务器端使用TP3.2处理(随便写在一个Controller里面) /* 小程序报名,生成订单 */ public function make_order(){ if(IS_POST){ $dat ...
- PHP关于进程池的优化
本文打算从另一个角度来讨论问题,教大家如何配置高效的环境,如此同样能够达到优化的目的. pool 一个让人沮丧的消息是绝大多数 PHP 程序员都忽视了池的价值.这里所说的池可不是指数据库连接池之类的东 ...
- Eclipse中导入项目后js报错解决方法(转未解决问题)
本文转自:http://blog.csdn.net/chenchunlin526/article/details/54666882 Eclipse中导入项目后js报错的原因与解决方法 在我们将项目导入 ...
- Python使用chardet包自动检测编码
chardet:charset detection 一旦自动检测出编码,就可以解码了. 八种文件打开方式 w:一旦打开文件,文件内容就清空了 r:只读方式打开 a:追加方式打开 r+:先读后写 以上四 ...