Skip to content
PHP Ecosystem
Explore

ActionFilter

API reference › yii › base › ActionFilter

classyii\base\ActionFilter
ExtendsBehavior » BaseObject
ImplementsConfigurable
Since2.0
Sourceframework/base/ActionFilter.php
9 subclasses

ActionFilter is the base class for action filters.

An action filter will participate in the action execution workflow by responding to the beforeAction and afterAction events triggered by modules and controllers.

Check implementation of AccessControl, PageCache and HttpCache as examples on how to use it.

For more details and usage information on ActionFilter, see the guide article on filters.

PropertyTypeDescriptionDefined by
$exceptarrayList of action IDs that this filter should not apply to. Default: [].ActionFilter
$onlyarrayList of action IDs that this filter should apply to. Default: [].ActionFilter
$owneryii\base\T|nullThe owner of this behavior Default: null.Behavior
MethodDescriptionDefined by
__construct()Constructor.BaseObject
__call()Calls the named method which is not a class method.BaseObject
__get()Returns the value of an object property.BaseObject
__isset()Checks if a property is set, i.e. defined and not null.BaseObject
__set()Sets value of an object property.BaseObject
__unset()Sets an object property to null.BaseObject
afterAction()This method is invoked right after an action is executed.ActionFilter
afterFilter()ActionFilter
attach()Attaches the behavior object to the component.ActionFilter
beforeAction()This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action.ActionFilter
beforeFilter()ActionFilter
canGetProperty()Returns a value indicating whether a property can be read.BaseObject
canSetProperty()Returns a value indicating whether a property can be set.BaseObject
className() staticReturns the fully qualified name of this class.BaseObject
detach()Detaches the behavior object from the component.ActionFilter
events()Declares event handlers for the $owner’s events.Behavior
hasMethod()Returns a value indicating whether a method is defined.BaseObject
hasProperty()Returns a value indicating whether a property is defined.BaseObject
init()Initializes the object.BaseObject
getActionId() protectedReturns an action ID by converting Action::$uniqueId into an ID relative to the module.ActionFilter
isActive() protectedReturns a value indicating whether the filter is active for the given action.ActionFilter
public array $only = []

List of action IDs that this filter should apply to.

If this property is not set, then the filter applies to all actions, unless they are listed in $except. If an action ID appears in both $only and $except, this filter will NOT apply to it.

Note that if the filter is attached to a module, the action IDs should also include child module IDs (if any) and controller IDs.

Since version 2.0.9 action IDs can be specified as wildcards, e.g. site/*.

public function afterAction(yii\base\Action $action, mixed $result): mixed

This method is invoked right after an action is executed.

You may override this method to do some postprocessing for the action.

Parameters

ParameterTypeDescription
$actionActionThe action just executed.
$resultmixedThe action execution result

Returns

mixed: The processed action result.

public function afterFilter(yii\base\ActionEvent $event): mixed

Parameters

ParameterTypeDescription
$eventActionEvent

Returns

mixed

public function attach(mixed $owner): mixed

Attaches the behavior object to the component.

The default implementation will set the $owner property and attach event handlers as declared in events(). Make sure you call the parent implementation if you override this method.

Parameters

ParameterTypeDescription
$ownermixedThe component that this behavior is to be attached to.

Returns

mixed

public function beforeAction(yii\base\Action $action): bool

This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action.

Parameters

ParameterTypeDescription
$actionActionThe action to be executed.

Returns

bool: Whether the action should continue to be executed.

public function beforeFilter(yii\base\ActionEvent $event): mixed

Parameters

ParameterTypeDescription
$eventActionEvent

Returns

mixed

public function detach(): mixed

Detaches the behavior object from the component.

The default implementation will unset the $owner property and detach event handlers declared in events(). Make sure you call the parent implementation if you override this method.

Returns

mixed

protected function getActionId(yii\base\Action $action): string

Returns an action ID by converting Action::$uniqueId into an ID relative to the module.

Parameters

ParameterTypeDescription
$actionAction

Returns

string

Since

2.0.7

protected function isActive(yii\base\Action $action): bool

Returns a value indicating whether the filter is active for the given action.

Parameters

ParameterTypeDescription
$actionActionThe action being filtered

Returns

bool: Whether the filter is active for the given action.