我们如何在 Laravel 5 中制作过滤器?过滤器的想法会消失吗?
8 回答
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.
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.
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.
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 Laravel 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 Update: Middleware parameters are available starting with Laravel 5.1.
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.
创建一个中间件
php artisan make:middleware class_name
在您的 app/Providers/RouteServiceProvider.php 中创建一个速记键:
protected $middleware = [ // .... 'shortName' => 'App\Http\Middleware\class_name', ];
您现在可以将它启用到任何路由(就像 L4 过滤器一样):
$router->post('url', ['middleware' => 'shortName', function() { ... }]);
似乎中间件正在取代 Laravel 的过滤器。至于你的问题。正确的答案是Middlewares。把它想象成层。
有关更详细的答案,请查看此内容。
旧答案
快速搜索被证明requests
是一种新的验证方式。但我不确定您的用例是否适用于此。
Laravel 5 引入了“请求”的概念。这是对您将作为 HTTP 请求的一部分执行的逻辑的总结,但不仅仅是一个路由过滤器。主要候选人:数据验证。
进行预验证(过滤器)的一种方法是使用方法authorize()
。
<?php namespace App\Http\Requests\Auth;
use Illuminate\Foundation\Http\FormRequest;
class RegisterRequest extends FormRequest {
public function rules()
{
return [
'email' => 'required|email|unique:users',
'password' => 'required|confirmed|min:8',
];
}
public function authorize()
{
return true;
}
}
有一个 rules() 方法可以返回一个规则数组,您可以在传递给 Validator::make() 之前使用它,还有一个 authorize() 方法,您可以在其中提供任何用户授权。通常您希望所有用户都能够注册,所以您只需返回 true。
对于之前/之后的评论。
从上面的链接:
在中间件..
#Before
public function handle($request, Closure $next)
{
//Do stuff
return $request;
}
#After
public function handle($request, Closure $next)
{
$response = $next($request);
// Do stuff {on $response}
return $response;
}
使用['middleware' => 'shortName']
应该相应地对待它。
filters.php 已被移除并替换为 routes.php 旁边的 Kernel.php
protected $routeMiddleware = [
'auth' => 'App\Http\Middleware\Authenticate',
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
];
但是你不能直接直接添加你的过滤器代码,你应该先创建一个中间件类(app/Http/Middleware)然后在Kernel.php文件中但是你想要的key并引用它自己的中间件类,例如:
'product.check' => 'App\Http\Middleware\ProductChecker'
是的,中间件是正确的地方,来自Laravel 5.0 文档:
HTTP 中间件提供了一种方便的机制来过滤进入应用程序的 HTTP 请求。
我个人认为添加中间件是一个很好的做法,但如果你碰巧需要一个快速的小型过滤控制器,rubyonrails 风格,
执行以下操作:
class myController{
//filters
public function myFilter()
{
//my filter's logic
}
public function __construct()
{
$this->myFilter();
//middlewares or any other code
}
}
现在 laravel 5 引入了中间件,而不是 laravel 4 中的过滤器。我建议你关注 laravel 5官方文档。