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 拾遗 中间件的更多相关文章

  1. Laravel 使用中间件进行权限控制

    Laravel 使用中间件进行权限控制 飞凡的陀螺 关注 2018.01.24 17:45 字数 264 阅读 1138评论 0喜欢 1 先看 文档Laravel 中间件提供了一种方便的机制来过滤进入 ...

  2. laravel StartSession中间件的实现原理

    1. 打开app\Http\Kernel.php,找到StartSession的位置.这里要说一下,middleware中的中间件是都会被执行的,但执行的顺序我不知道,还需看源码来实现 protect ...

  3. Laravel之中间件

    一.中间件的作用 HTTP 中间件提供了一个便利的机制来过滤进入应用的 HTTP 请求.例如,Laravel 包含了一个中间件来验证用户是否经过授权,如果用户没有经过授权,中间件会将用户重定向到登录页 ...

  4. laravel的中间件创建思路

    网上有很多解析laravel中间件的实现原理,但是不知道有没有读者在读的时候不明白,作者是怎么想到要用array_reduce函数的? 本文从自己的角度出发,模拟了如果我是作者,我是怎么实现这个中间件 ...

  5. 深入解析Laravel的中间件

    Laravel 中间件是什么? 简而言之,中间件在 laravel 中的作用就是过滤 HTTP 请求,根据不同的请求来执行不同的逻辑操作. 我们可以通过中间件实现以下功能: 指定某些路由 设置 HTT ...

  6. laravel 加中间件的方法 防止直接打开后台

    路由 routes.php Route::group(['middleware' => ['web','admin.login.login']], function () { //后台首页路由 ...

  7. Laravel 5 中间件、路由群组、子域名路由、 权限控制的基本使用方法

    创建控制器: php artisan make:controller Admin/IndexController 创建Middleware: php artisan make:middleware T ...

  8. Laravel 利用中间件控制权限。

    1.把所有需要控制的url写到config/admin.php里. 2.http/kernel.php文件里加上中间件. 3.http/Middleware/Permission.php 4.视图层

  9. laravel在中间件内生成的变量如何传到控制器

    在中间件内获取到一个变量,如何返回到控制器中并使用这个变量! 做了个demo: // web.php Route::get('/check', 'CheckController@check')-> ...

随机推荐

  1. Mapreduce实例-Top Key

    1 public class TopK extends Configured implements Tool { public static class TopKMapper extends Mapp ...

  2. MISRA-C 2012 C90规范和C99规范对比

  3. c++中的友元函数

    1.友元函数的简单介绍 1.1为什么要使用友元函数 在实现类之间数据共享时,减少系统开销,提高效率.如果类A中的函数要访问类B中的成员(例如:智能指针类的实现),那么类A中该函数要是类B的友元函数.具 ...

  4. git删除本地的资源,如何恢复?

    1.$ git reset --hard HEAD  将提交重置 2.使用 $ git checkout TestTimer.java(文件名) 恢复过来了

  5. SAP SQ01怎样从开发机传输到生产机

    确认你的Query是本地的还是全局的(跨Client).假设是后者,会自己主动生成传输请求,用标准传输方式就可以. 假设是本地的Query,有两种方式: 方式1:复制成全局的,让后生成传输请求 方式2 ...

  6. ActiveMq C#客户端 消息队列的使用(存和取)

    1.准备工具 VS2013Apache.NMS.ActiveMQ-1.7.2-bin.zipapache-activemq-5.14.0-bin.zip 2.开始项目 VS2013新建一个C#控制台应 ...

  7. redis阻塞bgsave与bsrewriteaof

    问题描述: redis在进程偶尔会出现2个进程redis-server \ redis-bgsave Redis 首先 fork 一个子进程, 并在该子进程里进行归并和写持久化存储设备(如硬盘)的. ...

  8. 【微信小程序】用户首次进入小程序拒绝授权,如何再次调用授权页面,获取用户信息userInfo

    前言:微信小程序的app.js里面,最少有2个接口,一个wx.login:一个是wx.getUserInfo: 前者得到腾讯给我们的微信用户唯一的code,通过code获取openid,这个不需要用户 ...

  9. mysql中的order by

    一.order by的原理 1.利用索引的有序性获取有序数据 当查询语句的 order BY 条件和查询的执行计划中所利用的 Index 的索引键(或前面几个索引键)完全一致,且索引访问方式为 ran ...

  10. 使用CXF实现基于Rest方式的WebService(转)

    转自:https://www.cnblogs.com/zjm701/p/6845813.html原文更清晰 本文介绍使用CXF实现基于Rest方式的WebService(CXF的版本是3.0.0) 一 ...