Yii2 覆盖 Rest ActiveController 中的通用创建操作 [英] Yii2 Override generic create Action in Rest ActiveController

查看:44
本文介绍了Yii2 覆盖 Rest ActiveController 中的通用创建操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Yii2 中有一个 Rest API,Yii 生成所有动作

I have a Rest API in Yii2, and Yii generates all actions

查看/更新/创建/删除

view / update / create / delete

我想改变createMethode et garde其他方法的comportement,所以我不能使用类Controller,我应该使用类ActiveController

I want to change the comportement of createMethode et garde the other methods, SO I can't use the class Controller, I should use the class ActiveController

但我希望那个类做同样的工作,我需要在创建之前添加一些动作,在创建之后添加一些动作.所以我需要覆盖 actionCreate

But I want that class do the same work, I need to add some action before creating and some actions after creating. So I need to override the actionCreate

我该怎么做?

推荐答案

您可以执行以下操作

class CountryController extends ActiveController
{
    public $modelClass = 'common\models\Country';

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['create']);
        return $actions;
    }

    public function actionCreate(){
        // implement here your code

    }

}

这篇关于Yii2 覆盖 Rest ActiveController 中的通用创建操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆