Route Filters
Route Filters
The Controller's Middleware, represents a High-Level processing API, executed by the requested Controller, when it is instantiated, its requested Method is known as being valid and callable, and working in the same flow as your wanted Method.
The graph of The Controller Execution Flow is as follow:
before() -> action() -> after()
While very efficient and accurate, sometimes this design is not the best. For example, to instantiate a Controller and start its Execution Flow, only to obtain a redirect from a CSRF fault, can be costly as resources and response speed.
Better is to have a solution for Routing to handle the CSRF fault, before to even instantiate the requested Controller, right after the Route was identified; this was the used resources will be lower and response speed will be better.
Enter the Route Filters: a method to execute specified callbacks right after the correct Route was identified and before starting the execution of associated Controller.
Route Filters
How do they work? Let's say that we want a CSRF filter. In the (new) file app/Filters.php we define it as following:
Route::filter('csrf', function($route) {
if (! Csrf::isTokenValid()) {
return Redirect::to('');
}
});
We'll see that a Route Filter definition have a name as first parameter and secondly, a callback which receive a Core\Route instance, which is just current matched Route, from where being available into callback information about HTTP method, URI, captured parameters, Route callback, etc.
ATTENTION: WHEN one of the Filters returns boolean FALSE, the Routing will generate a "404 Error" for the matched Route even it is a valid matched one.
This is useful to "hide" parts of your website for non-authenticated users or to redirect to a custom "404 Error" page, for example.
Note that Route Filters are defined using "Route::filter()"
How to use this Filter? We use a new style of defining Routes:
Router::post('contact', array(
'filters' => 'csrf',
'uses' => 'App\Controllers\Contact@store'
));
WHERE the Route definition accepts an array as a second parameter and where the keys name is obvious. The key filters' assign to the value of a '|' separated string of used Route Filters, and the key 'uses' assign the associated Callback for the Route.
Running this Route definition, the Routing will be known to apply the Filter with the name 'csrf' before the Controller execution, then on CSRF fault, the Filter's callback will be executed and we go very fast into a redirect.
It's possible to apply multiple Filters to a Route, using a string containing their name separated by character '|' (pipe).
Usually, we will want to add another two Route Filters and there is a more complex example:
Route::filter('csrf', function($route) {
if (($route->method() == 'POST') && ! Csrf::isTokenValid()) {
return Redirect::to('');
}
});
Route::filter('auth', function($route) {
if (Session::get('loggedIn') == false) {
return Redirect::to('login');
}
});
Route::filter('guest', function($route) {
if (Session::get('loggedIn') != false) {
return Redirect::to('');
}
});
And an example of their usage can be:
Router::any('contact', array(
'filters' => 'guest|csrf',
'uses' => 'App\Controllers\Contact@index'
));
Router::any('login', array(
'filters' => 'guest|csrf',
'uses' => 'App\Controllers\Auth@login'
));
Router::get('logout', array(
'filters' => 'auth',
'uses' => 'App\Controllers\Auth@logout'
));
WHERE only the only Guest Users can access the Contact and Login page, with CSRF validation, while only the Authenticated Users can access the Logout action.
The alternative usage of Route Filters registering is to use a Class instead of callback, where the called method will receive the matched Route instance as a parameter. For example:
Route::filter('auth', 'App\Helpers\Filters\User@isLoggedIn');
Route::filter('guest', 'App\Helpers\Filters\User@isGuest');
Improvements
An improved Method handling when the Routes are registered and a new Router command called share(), which permit to register multiple Routes all pointing to the same Controller.
For example:
Router::share(array(
array('GET', '/'),
array('POST', '/home')
), 'App\Controllers\Home@index');
Route Filters的更多相关文章
- laravel route路由,视图和response和filter
Laravel充分利用PHP 5.3的特性,使路由变得简单并富于表达性.这使得从构建API到完整的web应用都变得尽可能容易.路由的实现代码在 application/routes.php 文件. 和 ...
- kubernetes源码解析---- apiserver路由构建解析(1)
kubernetes源码解析---- apiserver路由构建解析(1) apiserver作为k8s集群的唯一入口,内部主要实现了两个功能,一个是请求的路由和处理,简单说就是监听一个端口,把接收到 ...
- Response
Response This improved Response API, able to simplify the Framework's Response management. Practical ...
- Authentication
Authentication Introduction Configuration Storing Passwords Authenticating Users Basic Usage Introdu ...
- laravel code bright
Project RootLet’s start by taking a look at the root folder structure.• app/• bootstrap/• vendor/• p ...
- Data Center手册(4):设计
基础架构 拓扑图 Switching Path L3 routing at aggregation layer L2 switching at access layer L3 switch融合了三种功 ...
- zuul1.3源码扒一扒(1)
先开个头吧 作为偶尔点进源码的时候看到东西,或是学到,或是不解,或是惊讶,之后的一些记录.从springcloud各个组件开始吧,计划文段保持间断,只道出核心点,不过各个文段保持连续. zuul作为s ...
- 深入理解Zuul之源码解析
转载:http://blog.csdn.net/forezp/article/details/76211680 Zuul 架构图 在zuul中, 整个请求的过程是这样的,首先将请求给zuulservl ...
- spring cloud连载第三篇补充之Zuul
由于Zuul的内容较多所以单独列出一篇来讲.全是干货,如果学到东西的,动动小手给点个推荐^_^ 谢谢! 1. Router and Filter: Zuul(路由和过滤:Zuul) 路由是微服务架构 ...
随机推荐
- (1)java虚拟机概念和结构图
java虚拟机解构图一 java虚拟机解构图二 java虚拟机结构图三 [1]类加载系统 --->负责从文件系统或网络中加载class信息,存放至方法区的内存空间[2]java堆 ...
- Hibernate之HQL介绍
Hibernate中提供了多种检索对象的方式,主要包括以下种类: 导航对象图检索方式:根据已经加载的对象导航到其他对象 OID检索方式:根据对象的OID来检索对象 HQL检索方式:使用面向对象的HQL ...
- Collection Of SVM Libraries By Language via datasciencecentral
http://www.datasciencecentral.com/profiles/blogs/collection-of-svm-libraries-by-language Support vec ...
- SGU 134 Centroid
题意:给出一个树,每个点有一个value,value的意义是去掉这个点之后所有连通分量中点最多的那个连通分量的点数,这棵树的重心为所有点value的最小值,求重心,及重心都有谁. 解法:貌似是个树形d ...
- Python日期时间函数处理
所有日期.时间的 api 都在datetime模块内. 1 日期的格式化输出 datetime => string import datetime now = datetime.datetime ...
- Stm32 SWD 下载 调试配置
找到一篇比较好的 关于stm32 SWD模式 下载 调试 配置文章 整理如下: 我们比较常用的是Jlink下载器 ,这种下载器有一个缺点就是使用的Jtag 20PIN接口,太多的PIN会 ...
- gimp之旅
随着大学生活的告一段落,新的征途已经开始了.鉴于本人如此喜欢旅游,如此喜欢拍照,如此喜欢处理图片,所以打算在照片处理上下点功夫.总所周知,图像处理软件大牛级的就属windows下的photoshop以 ...
- [假期总结]Self
8月底返校,这一周也没有勤学苦练.假期3周的时间学习了nodejs. 1.nodejs的学习 学习了一本书上的例程,搭建了个博客.这种程度等于是能够动手开发的阶段,前路还很漫长. 2.个人网站的建设 ...
- 怎样让你的代码更好的被JVM JIT Inlining
好书推荐:Effective Java中文版(第2版) JVM JIT编译器优化技术有近100中,其中最最重要的方式就是内联(inlining).方法内联可以省掉方法栈帧的创建,方法内联还使让JIT编 ...
- UITableView 详解 教程
看TableView的资料其实已经蛮久了,一直想写点儿东西,却总是因为各种原因拖延,今天晚上有时间静下心来记录一些最近学习的TableView的知识.下面进入正题,UITableView堪称UIKit ...