mikatakana
mikatakana

Reputation: 523

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?

Upvotes: 11

Views: 3565

Answers (2)

Tema Smirnov
Tema Smirnov

Reputation: 21

You can just normally use Route::resource() constructor. Use this code:

Route::resource('items', 'ItemsController', ['before' => 'csrf']);

Upvotes: 0

msturdy
msturdy

Reputation: 10794

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.

Upvotes: 12

Related Questions