Action
API reference › yii › base › Action
7 subclasses
Action is the base class for all controller action classes.
Action provides a way to reuse action method code. An action method in an Action class can be used in multiple controllers or in different projects.
Derived classes must implement a method named run(). This method will be invoked by the controller when the action
is requested.
The run() method can have parameters which will be filled up with user input values automatically according to
their names.
For example, if the run() method is declared as follows:
public function run($id, $type = 'book') { ... }And the parameters provided for the action are: ['id' => 1].
Then the run() method will be invoked as run(1) automatically.
For more details and usage information on Action, see the guide article on actions.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$behaviors read-only | array<Behavior<static>> | List of behaviors attached to this component. | Component |
$controller | yii\ | The controller that owns this action, or null when the action runs standalone. via Module::$actionMap Default: null. | Action |
$id | string| | ID of the action; null between construction and dispatcher post-assignment for standalone actions resolved through Module::$actionMap or Module::$actionNamespace. Default: null. | Action |
$module | Module| | The owning module, or null if neither a module nor a controller is attached. Available since 22.0. | Action |
$uniqueId read-only | string | The unique ID of this action among the whole application. | Action |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | Action |
__call() | Calls the named method which is not a class method. | Component |
__clone() | This method is called after the object is created by cloning an existing one. | Component |
__get() | Returns the value of a component property. | Component |
__isset() | Checks if a property is set, i.e. defined and not null. | Component |
__set() | Sets the value of a component property. | Component |
__unset() | Sets a component property to be null. | Component |
attachBehavior() | Attaches a behavior to this component. | Component |
attachBehaviors() | Attaches a list of behaviors to the component. | Component |
behaviors() | Returns a list of behaviors that this component should behave as. | Component |
canGetProperty() | Returns a value indicating whether a property can be read. | Component |
canSetProperty() | Returns a value indicating whether a property can be set. | Component |
className() static | Returns the fully qualified name of this class. | BaseObject |
detachBehavior() | Detaches a behavior from the component. | Component |
detachBehaviors() | Detaches all behaviors from the component. | Component |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | Component |
getBehavior() | Returns the named behavior object. | Component |
getBehaviors() | Returns all behaviors attached to this component. | Component |
getModule() | Returns the module that owns this action when invoked standalone. | Action |
getUniqueId() | Returns the unique ID of this action among the whole application. | Action |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | Component |
hasMethod() | Returns a value indicating whether a method is defined. | Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | Component |
init() | Initializes the object. | BaseObject |
off() | Detaches an existing event handler from this component. | Component |
on() | Attaches an event handler to an event. | Component |
runWithParams() | Runs this action with the specified parameters. | Action |
setModule() | Sets the module that owns this action when invoked standalone. | Action |
trigger() | Triggers an event. | Component |
afterRun() protected | This method is called right after run() is executed. | Action |
beforeRun() protected | This method is called right before run() is executed. | Action |
resolveStandaloneParams() protected | Resolves typed parameters of run() when the action is invoked without a hosting controller. | Action |
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(string|null $id = null, yii\base\T|null $controller = null, array<string, mixed> $config = [])Constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | string| | The ID of this action, or null when the action is instantiated through the DI container and the ID is assigned afterwards by the dispatcher. |
$controller | yii\ | The controller that owns this action, or null for standalone handlers. |
$config | array<string, | Name-value pairs that will be used to initialize the object properties. |
getModule()
Section titled “getModule() ”public function getModule(): yii\base\Module|nullReturns the module that owns this action when invoked standalone.
Falls back to the controller’s module when a controller hosts the action.
Returns
Module|null: The owning module, or null if neither a module nor a controller is attached.
Since
22.0
getUniqueId()
Section titled “getUniqueId() ”public function getUniqueId(): stringReturns the unique ID of this action among the whole application.
When the action is hosted by a controller, the ID is composed of the controller’s unique ID and the action ID.
For standalone handlers (no controller), the ID falls back to the owning module’s unique ID, or to the action ID alone when no module is attached.
Returns
string: The unique ID of this action among the whole application.
runWithParams()
Section titled “runWithParams() ”public function runWithParams(array $params): mixedRuns this action with the specified parameters.
When a controller hosts the action, parameter binding is delegated to Controller::bindActionParams().
For standalone handlers the typed parameters of run() are resolved through the DI container via
resolveStandaloneParams().
Parameters
| Parameter | Type | Description |
|---|---|---|
$params | array | The parameters to be bound to the action’s run() method. |
Returns
mixed: The result of the action.
Throws
InvalidConfigException: if the action class does not have arun()method.
setModule()
Section titled “setModule() ”public function setModule(yii\base\Module|null $module): voidSets the module that owns this action when invoked standalone.
Used by Module::runAction() when dispatching a handler registered in Module::$actionMap.
Parameters
| Parameter | Type | Description |
|---|---|---|
$module | Module| | The owning module, or null to detach. |
Since
22.0
afterRun()
Section titled “afterRun() ”protected function afterRun(): mixedThis method is called right after run() is executed.
You may override this method to do post-processing work for the action run.
Returns
mixed
beforeRun()
Section titled “beforeRun() ”protected function beforeRun(): boolThis method is called right before run() is executed.
You may override this method to do preparation work for the action run. If the method returns false, it will cancel the action.
Returns
bool: Whether to run the action.
resolveStandaloneParams()
Section titled “resolveStandaloneParams() ”protected function resolveStandaloneParams(array $params): array<array-key, mixed>Resolves typed parameters of run() when the action is invoked without a hosting controller.
Delegates to Container::resolveCallableDependencies() so that route parameters are matched by name
and class-typed parameters are autowired from the DI container.
Parameters
| Parameter | Type | Description |
|---|---|---|
$params | array | Route parameters to be matched by name against run()’s signature. |
Returns
array<array-key, mixed>: The resolved positional argument list for run().
Since
22.0