我在使用restful控制器。我需要运行一些过滤器,如auth和自定义权限。所以我把他们分成了一个路由组,并在那组上设置过滤器。此外,我还想运行csrf过滤器,但只对post请求。如何在路线组中做到这一点?
添加澄清代码
Route::group(array('before' => 'auth|allowed|csrf'), function() {
Route::controller('controller', 'SomeController');
Route::controller('othercontroller', 'OtherController');
});我要中央应急基金只在邮政路线上。我真的不想在每个控制器上添加一个过滤器(有相当多的);
发布于 2013-09-20 18:24:00
好的。我想我解决了。我检查了请求是否是邮寄的。不知道这是不是不好的做法。我将filter.php中的csrf过滤器更改为
Route::filter('csrf', function()
{
if (Request::getMethod() == 'POST' && Session::token() != Input::get('_token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
});发布于 2013-09-19 16:31:20
在使用资源丰富的路由时,您可以从控制器中执行此操作。
public function __construct() {
$this->beforeFilter('csrf', array('on' => 'post'));
}发布于 2013-12-02 20:12:11
您可以创建一个运行标准CSRF过滤器的自定义过滤器,但只能在这样的POST请求下运行.
Route::filter('csrfIfPost', function($route, $request) {
if ($request->getMethod() == 'POST')
return $route->callFilter('csrf', $request);
});然后在任何你想使用它的地方,只使用过滤器'csrfIfPost‘而不是'csrf’。
https://stackoverflow.com/questions/18900130
复制相似问题