Reputation: 523
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
Reputation: 21
You can just normally use Route::resource()
constructor. Use this code:
Route::resource('items', 'ItemsController', ['before' => 'csrf']);
Upvotes: 0
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