Skip to content

Controller

filters()

filters() — returns void

Available in: controller Category: Configuration Functions

The filters() function lets you specify methods in your controller that should run automatically either before or after certain actions. Filters are useful for handling cross-cutting concerns such as authentication, authorization, logging, or cleanup, without having to repeat the same code inside each action. By default, filters run before the action, but you can configure them to run after, limit them to specific actions, exclude them from others, or control their placement in the filter chain.

NameTypeRequiredDefaultDescription
throughstringyesFunction(s) to execute before or after the action(s).
typestringnobeforeWhether to run the function(s) before or after the action(s).
onlystringnoPass in a list of action names (or one action name) to tell Wheels that the filter function(s) should only be run on these actions.
exceptstringnoPass in a list of action names (or one action name) to tell Wheels that the filter function(s) should be run on all actions except the specified ones.
placementstringnoappendPass in prepend to prepend the function(s) to the filter chain instead of appending.
1. Run a filter before all actions
// Always execute restrictAccess before every action
filters("restrictAccess");

2. Multiple filters before all actions
// Run both isLoggedIn and checkIPAddress before all actions
filters(through="isLoggedIn, checkIPAddress");

3. Exclude specific actions
// Run filters before all actions, except home and login
filters(through="isLoggedIn, checkIPAddress", except="home, login");

4. Limit filters to specific actions
// Only run ensureAdmin before the delete action
filters(through="ensureAdmin", only="delete");

5. Run filters after an action
// Run logAction after every action
filters(through="logAction", type="after");