Home >Backend Development >PHP Tutorial >Yii action method techniques_PHP tutorial
Action is defined as a method named with the word action as the prefix. A more advanced way is to define an action class and have the controller instantiate it when it receives a request. This allows actions to be reused, improving reusability.
1. Define an action class. The basic format is as follows:
class UpdateAction extends CAction{ public function run(){ //...逻辑代码... } }
2. Use the action class: In order to let the controller notice this action, we need to override the actions() method of the controller class in the following way:
class PostController extends CController{ public function actions(){ return array( //使用"应用程序文件夹/controllers/post/UpdateAction.php" 文件中的类来处理edit动作 'edit'=>'application.controllers.post.UpdateAction', ); } }
As shown above, we used the path alias application.controllers.post.UpdateAction to specify the action class file as protected/controllers/post/UpdateAction.php
By writing class-based actions, we can organize the application into a module style.