Laravel 4除了控制器构造函数中的过滤器

编程入门 行业动态 更新时间:2024-10-10 02:18:02
本文介绍了Laravel 4除了控制器构造函数中的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当前,我有一个带有构造方法的AdminContoller,该方法处理一些以前的过滤器.是否可以对除一个以外的所有控制器方法进行事前过滤?

Currently I have an AdminContoller with a construct method handling some of the before filters. Is there a way to do a before filter on all controller methods except one?

我正在使用Entrust的角色和权限,但是这段代码使我陷入无限重定向循环.我根本没有以用户身份登录.因此,此代码应将我重定向到/admin/login URL,该URL附加到未过滤的AdminController @ adminLogin方法.但这不是吗?

I'm using Entrust for Roles and Permissions, but this code is throwing me into an infinite redirect loop. I'm not logged in as a user at all. So this code should redirect me to the /admin/login url which is attached to an unfiltered AdminController@adminLogin method. But it doesn't?

//AdminController.php文件

// AdminController.php file

class AdminController extends BaseController { function __construct() { // Is something like this possible? $this->beforeFilter('admin', array('except' => array('adminLogin'))); $this->beforeFilter('csrf', array('on' => 'post')); } public function index() { return "Admin - Index"; } public function adminLogin() { return "Admin Login Form"; } // ... and many more methods }

//Filter.php文件

// Filter.php file

Route::filter('admin', function() { if( !Entrust::hasRole('admin') ) // Checks the current user { return Redirect::to('/admin/login'); } });

//Routes.php文件

// Routes.php file

Route::resource('admin', 'AdminController'); Route::get('/admin/login', 'AdminController@adminLogin');

推荐答案

在将新方法添加到资源丰富的控制器中后,您应该先在资源之前注册新方法.

As you've added a new method into a resourceful controller, you should register the new method first, before the resource.

例如

<?php // Routes.php Route::get('/admin/login', 'AdminController@adminLogin'); Route::resource('admin', 'AdminController');

这样,您的before过滤器应可以像这样正常工作:

This way your before filters should work as you have then like this:

<?php // AdminController.php class AdminController extends BaseController { function __construct() { $this->beforeFilter('admin', array('except' => array('adminLogin'))); $this->beforeFilter('csrf', array('on' => 'post')); } }

更多推荐

Laravel 4除了控制器构造函数中的过滤器

本文发布于:2023-11-17 11:13:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609829.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   控制器   函数   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!