Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I put laravel 4 filter classes?

In laravel 4, you can create filter classes instead of putting the entire filter inside a closure -- great. But do these filters have to be entirely in the app/filters.php or app/routes.php?

Generally I like to do one file per class, but I imagine there's something better to do then a bunch of includes in the filters.php file. Where would you put these for laravel to find them automatically? For example:

Route::filter('Thing', 'ThingFilter');

# can I put this in its own file and have laravel automatically use it?
class ThingFilter {
    function filter() { ... }
}
like image 327
just.another.newbie Avatar asked Apr 26 '13 00:04

just.another.newbie


1 Answers

I've all my filters in a separate directory called filters. And here's how my filters.php file look like...

//---------------------------------------------------------
// Route Filters
//---------------------------------------------------------
Route::filter('auth', 'AuthFilter@default');
Route::filter('auth.basic', 'AuthFilter@basic');
Route::filter('guest', 'AuthFilter@guest');
Route::filter('csrf', 'CsrfFilter');

I autoload them via composer.json

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/filters",
        "app/presenters",
        "app/repositories",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ]
},

After you update your composer.json file, you need to run the command

composer dump-autoload

To verfiy that you files will be loaded, check out

vendor/composer/autoload_classmap.php
like image 59
Ashit Vora Avatar answered Oct 01 '22 22:10

Ashit Vora