Route Filters「建议收藏」

Route Filters「建议收藏」RouteFiltersTheController’sMiddleware,representsaHigh-LevelprocessingAPI,executedbytherequestedController,whenitisinstantiated,itsrequestedMethodisknownasbeingvalidandca…

大家好,又见面了,我是你们的朋友全栈君。

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');

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143213.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • How to make transparent bridge with Slackware Linux.

    How to make transparent bridge with Slackware Linux.Whatisatransparentbridgeandwhytouseit?Fewlinesofdrytheoryfirst:Transparentbridgesareusedforvarioustestsandsecurityapplications.Sniffingtraffic.(Ididthisalot when

    2022年7月21日
    14
  • pki密码技术_PKI体系管理

    pki密码技术_PKI体系管理HTTPS的诞生可先参考网络协议、HTTPS协议等文章明文传输对称加密“加密”和“解密”使用【相同的】密钥,如果密钥可以安全的传输,那么消息也应该可以安全的传输。非对称加密上述非对称加密与对称加密效果基本一样,如果公钥可以安全的传输,那么消息也应该可以安全的传输,接下来看看被劫持的情况。窃听者可以伪造服务器的公钥与客户端通讯,客户端以为是跟服务器通讯,其实是与窃听者在通讯。无论是对称加密还是非对称加密,都遗留了一个问题没有解决,那就是如何证明我们访问的网站就是我们

    2022年8月22日
    6
  • Lighttpd 插件mod_h264 streaming (mp4)安装

    Lighttpd 插件mod_h264 streaming (mp4)安装

    2022年3月5日
    40
  • redis如何设置密码及验证密码_redis如何设置密码及验证密码

    redis如何设置密码及验证密码_redis如何设置密码及验证密码在百度云安装redis服务之后,一直给我发送系统安全警告,推荐我redis设置访问密码,于是出于安全考虑我就设置一下redis的密码1.修改redis.conf配置文件:找到requirepass这一行,解注这一行代码,requirepass后面就是跟的自己的密码。2.关闭redis服务,发现报错:可以使用下面两个方法关闭服务:方式一:通过psaux|grepred…

    2025年9月15日
    6
  • 游戏建模:手绘暗黑小萝莉「建议收藏」

    平时喜欢画画,最近呐再做一些个人作品。然后再A站各种淘原画,从一堆原画中挑选自己喜欢的,把它画成模型。然后发现选的原画每次都给自己挖了很多的坑。在选这张原画的时候想的是,小萝莉卡哇伊。一身金闪闪发光blingbling的,贼漂酿。头发直接就是一个条一个条的,全身除了头上的装饰还有肩部的就没啥了,还是ok的。但是在实际的模型制作还有贴图制作中发现,emmmm,有一个深坑,快爬不出来了。谁来救一下可爱的小哥哥。下面呐就是我选的卡哇伊小姐姐。挑战一下这种非正常肤色。(图1原画)下面呐…

    2022年4月11日
    50
  • DWD层总结

    DWD层总结DWD层:4步建模作用:1)对用户行为数据进行解析2)对核心数据进行判空过滤3)对业务数据采用维度模型重新建模。一、DWD层数据分析首先DWD层数据都来源于ODS层。具体数据可分为两类1)用户行为数据(多为json)2)业务数据1、用户行为数据业务行为数据一般都是来源于前端页面的埋点日志信息分为启动日志和普通日志启动日志表中每行数据对应一个启动记录,一个启动记录应该包含日志中的公共信息和启动信息。先将所有包含start字段的日志过滤出来,然后使用get_json_object

    2022年6月26日
    53

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号