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() { ... }
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With