简体   繁体   中英

Yii Override Generic Rest Functions

I am new to yii2. I want to write restful api in yii framework. From 3 days of searching I am able to run restful services example. I found that yii generates these methods automatically index, view, create, update, delete. What will I do if I want to customize index, create, update and delete method? Because I have only found method to customize output of index function which is prepareDataProvider. And what will I do if I add new method? Or is it better to write your own custom methods if yii not provide customization of create, update and delete method.

You can always override the original behavior by defining the actions() method in your ActiveController class.

Here is an example:

 public function actions()
 {
     return array_merge(parent::actions(), [
         'create' => null, // Disable create
         'view' => [
             'class' => 'yii\rest\ViewAction',
             'modelClass' => $this->modelClass,
             'checkAccess' => [$this, 'checkAccess'],
             'findModel' => ['\path\to\your\callback', 'findModelByYourOwnMethod'],
         ],
         'update' => [
             'class' => 'path\to\your\UpdateAction',
             'modelClass' => $this->modelClass,
             'checkAccess' => [$this, 'checkAccess'],
             'scenario' => SCENARIO_UPDATE,
         ],
     ]);
 }

You can define your own action class, scenario, etc. If you want to override some but not all actions, remember to merge with parent::actions() .

EDIT

In general, you can always add extra actions in the controller by 1) declaring method actionX() , where X is the name of action; and/or 2) declaring method actions() which returns array of action configuration, just like the example above. And Yii2 framework provides a special controller \\yii\\rest\\ActiveController which provides implemented actions for CRUD and index.

If you read the source code, you will notice that in ActiveController , there is actions() method which defined something similar to the example above. Each action has linked to a class under the same package, for example, 'index' => ['class' => 'yii\\rest\\IndexAction'] . Implementation in every action is different, and some of them provides an optional callable variable for developer to set and override original behavior. IndexAction , for example, provides $prepareDataProvider; for you to override; and you can see from my example, $findModel is another one that appears in every rest action.

I am not going to put the list of such callable variable here unless there are strong needs from the community. To understand what is needed to fill in in order to extend the rest action behavior, please check the source code under rest folder of the framework: https://github.com/yiisoft/yii2/tree/master/framework/rest ; and FYI, I have another answer which may help you understand more: Yii2 Restful API - Example to Add a New Action

I know this is old, but I feel this question is very similar to one I've answered. It would be worthwhile to check out my answer here: https://stackoverflow.com/a/50744982/3337682

Hope it helps someone.

~ Cheers :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM