Before_Filter With Parameters

before_filter with parameters

I'd do it like this:

before_filter { |c| c.authenticate_rights correct_id_here }

def authenticate_rights(project_id)
project = Project.find(project_id)
redirect_to signin_path unless project.hidden
end

Where correct_id_here is the relevant id to access a Project.

Laravel 4 Call filter in controller with parameters

You can pass a parameter as a string after your filter name. Use : to separate method name and parameter.

public function __construct()
{
$this->beforeFilter('filterSomething:param');
}

If you have many parameters, you can use , to separate your parameters.

$this->beforeFilter('filterSomething:param1,param2');

And then define your filter logic in app/filters.php. Don't forget to add parameter in your filter method for passed parameters.

Route::filter('filterSomething', function($route, $request, $param1, $param2)
{
// Do something with your params.
});

beforeFilter() not being called?

I forgot to call the base class' beforeFilter() since I was overloading it in my ImagesController

class ImagesController extends AppController {

var $name = 'Images';
var $helpers = array('TinyMCE');

function beforeFilter(){
parent::beforeFilter(); // <-- here
if(!isset($this->params['admin'])) {
$this->Session->setFlash(__('Access denied.', true));
$this->redirect(array('controller'=>'users','action'=>'login','admin'=>false));
exit();
}
}

function index() {
$this->Image->recursive = 1;
$this->set('images', $this->paginate());
}

}

laravel 4 where the beforefilter's second parameter pass to?

You are looking at the wrong place. That array parameter is not being passed to the auth filter. It's a parameter for Controller.beforeFilter().

As you can see in Laravel's documentation, beforeFilter() expects two parameters:

  1. $filter The filter to be executed before your controller actions. These filters are the ones that you see in filters.php file.
  2. $options An array with options for the filter being passed. In this case, you are passing the except option with a value of getLogin.

/**
* Register a new "before" filter on the controller.
*
* @param string $filter
* @param array $options
* @return void
*/
public function beforeFilter($filter, array $options = array())
{
$options = $this->prepareFilter($filter, $options);

$this->filters[] = new Before($options);
}

cakephp $this- params not set in beforeFilter

My bad, I had overriden beforeFilter in an controller and forgot to call parent::beforeFilter() at the start. Now it's ok.

Laravel Before Filter in the Controller with arguments

I can confirm what happens... when you add a filter to a controller, any arguments passed to the action are added to the front of the filter's arguments. You can use func_get_args() to confirm that this is the case.

However, when you add a filter to a route this is not the case, the filter is run without any additional arguments.

So I can see 2 choices here, either add the filter to the route...

Route::get('test/(:any)', array('before' => 'permission:destroy|auth', 'use' => 'test@show'));

Or modify your filter to use func_get_args(), like...

Route::filter('permission', function()
{
$args = func_get_args();
$permission = array_pop($args);

});

before_filter with parameters and private methods

It's not nice, as already was stated, but usage of .send does help and it is also mentioned in the Action Controller Overview Guide, therefore I think there is no real better way.

How can I send a parameter to a before filter?

You should be able to do this with a block:

before_filter {|controller| controller.check_role('admin') }


Related Topics



Leave a reply



Submit