Home >Backend Development >PHP Tutorial >In what scenarios are independent operations in Yii generally used?
Under what circumstances is this kind of controller-independent operation usually used? I’ve been thinking about it for a long time and haven’t found any place where this would be used in actual development (and I think this would lead to a coupling between the two controllers)
Independent operation
Independent operations are defined by inheriting yiibaseAction or its subclasses.
For example, the yiiwebViewAction and yiiwebErrorAction released by Yii are independent operations.To use independent operations, you need to declare it in the action
map by overriding the yiibaseController::actions() method in the controller, as shown in the following example:public function actions() {
<code>return [ // 用类来申明"error" 操作 'error' => 'yii\web\ErrorAction', // 用配置数组申明 "view" 操作 'view' => [ 'class' => 'yii\web\ViewAction', 'viewPrefix' => '', ], ]; } 如上所示, actions() 方法返回键为操作ID、值为对应操作类名或数组configurations 的数组。 和内联操作不同,独立操作ID可包含任意字符,只要在actions() 方法中申明. </code>To create an independent operation class, you need to inherit yiibaseAction or its subclass and implement the public method named run(). The role of the run()
method is similar to the operation method, for example:
use yiibaseAction;
class HelloWorldAction extends Action {
<code>public function run() { return "Hello World"; }</code>}
Under what circumstances is this kind of controller-independent operation usually used? I’ve been thinking about it for a long time and haven’t found any place where this would be used in actual development (and I think this would lead to a coupling between the two controllers)
Independent operation
Independent operations are defined by inheriting yiibaseAction or its subclasses.
For example, the yiiwebViewAction and yiiwebErrorAction released by Yii are independent operations.To use independent operations, you need to declare it in the action
map by overriding the yiibaseController::actions() method in the controller, as shown in the following example:public function actions() {
return [ // 用类来申明"error" 操作 'error' => 'yii\web\ErrorAction', // 用配置数组申明 "view" 操作 'view' => [ 'class' => 'yii\web\ViewAction', 'viewPrefix' => '', ], ]; } 如上所示, actions() 方法返回键为操作ID、值为对应操作类名或数组configurations 的数组。 和内联操作不同,独立操作ID可包含任意字符,只要在actions() 方法中申明.
To create an independent operation class, you need to inherit yiibaseAction or its subclass and implement the public method named run(). The role of the run()
method is similar to the operation method, for example:
use yiibaseAction;
class HelloWorldAction extends Action {
<code>public function run() { return "Hello World"; }</code>}
The verification code that comes with yii2 is a typical usage example