Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Filter for needed Resource Controller actions?

I have a resource controller Items:

Route::resource('items', 'ItemsController');

And in action ItemsController@store for creating new item i'm need to activate CSRF filter (and maybe Auth filter) before actions with form. But i can't write

$this->beforeFilter('csrf')

it doesn't works. It works when I put this calling in parent Controller's __construct().

What I need to do to call some filters directly for resource controller actions?

like image 797
mikatakana Avatar asked Aug 06 '13 09:08

mikatakana


1 Answers

You should be able to use the filters for specific actions only by setting it in the __construct() like this:

class ItemsController extends BaseController
{
    public function __construct()
    {
        $this->beforeFilter('csrf', array('on' => 'store') );
    }

    //rest of controller... 
}

Note - You can also use the keywords "only" and "except" and pass an array of action names to apply (or negate) the filter for more than one action.

like image 137
msturdy Avatar answered Nov 09 '22 23:11

msturdy