过滤器在Laravel 5 [英] Filters in Laravel 5

查看:78
本文介绍了过滤器在Laravel 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在Laravel 5中制作过滤器? 过滤器的想法会消失吗?

How do we make filters in Laravel 5? Is the idea of filters going away?

推荐答案

最简短的答案是不",路由过滤器在 中不会完全消失(在Laravel 5.0中(尽管有些)有关此的误导性信息).该功能仍然存在,可以根据需要在路由上使用之前"和之后"过滤器.不再提供"filters.php"文件,但您仍然可以在其他位置定义过滤器,可能最适合在Providers/RouteServiceProvider.php的boot()函数中使用.

The short answer is no, route filters are not going away entirely in Laravel 5.0 (despite some misleading information out there about this). The functionality still exists to let you use 'before' and 'after' filters on your routes if you would like. The "filters.php" file is no longer provided, but you can still define your filters somewhere else, probably most appropriately in the boot() function of Providers/RouteServiceProvider.php.

但是,中间件现在是实现相同功能的首选方式.有关如何使用它的信息,请参见 http://laravel.com/docs/master/middleware .

However, middleware is now the preferred way to achieve the same functionality. See http://laravel.com/docs/master/middleware for info about how to use it.

可以将中间件实现为类似于之前"或之后"过滤器.可以将其应用于所有路由(称为全局中间件"),或分配给特定的路由(例如,通过在"routes.php"文件中的路由定义中添加'middleware'=>'auth'").

Middleware can be implemented to behave like either "before" or "after" filters. And it can be applied to all routes (called "global middleware"), or assigned to specific routes (by adding "'middleware' => 'auth'" for example to your route definitions in your routes.php file.

中间件的唯一重大限制是它目前无法为您提供传递参数的方式(就像使用过滤器一样).这意味着您不能执行例如"requirePermission:admin"之类的操作.当前有两种方法可以解决此限制.您可以改为使用路由过滤器,就像使用Larvel 4.2一样.或者,如果您更喜欢使用中间件,则感觉有点像破解,但是您可以通过定义和检索添加到路由定义中的自定义值来将参数传递给中间件,如http://blog.elliothesp.co.uk/coding/passing-parameters-middleware-laravel-5/.

The only significant limitation of middleware is that it currently doesn't give you a way to pass parameters (as you can with filters). This means you can't do something like "requirePermission:admin" for example. There are currently two ways to deal with this limitation. You can instead just use a route filter instead, just as you did with Larvel 4.2. Or otherwise if you prefer using middleware, this feels like a bit of a hack, but you can pass parameters to the middleware by defining and retrieving custom values added to your route definition as explained at http://blog.elliothesp.co.uk/coding/passing-parameters-middleware-laravel-5/.

2015-05-29更新::中间件参数开始可用使用Laravel 5.1.

2015-05-29 Update: Middleware parameters are available starting with Laravel 5.1.

2015-06-10更新:路由过滤器已不推荐使用中间件,并将在2015年12月发布的Laravel 5.2中完全删除.

2015-06-10 Update: Route filters have been deprecated in preference of middleware and will be removed entirely with the release of Laravel 5.2 in December 2015.

这篇关于过滤器在Laravel 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆