douzhantao2857 2017-04-16 07:43
浏览 98

Yii2 ActiveController重写默认的post动作

I have no clue how to do it in YII2 and I didn't find anything in the docs. I've tried:

public function actionCreate() {
     //code   
}

my controller:

<?php
namespace app\controllers;

use yii\filters\auth\HttpBasicAuth;
use yiiest\ActiveController;

class TempController extends ActiveController
{
    public $modelClass = 'app\models\Event';

}
?>

How to rewrite default post action in a Yii2 ActiveController?

  • 写回答

1条回答 默认 最新

  • dswu26846 2017-04-16 10:21
    关注

    I assume by default post action you mean create action with POST HTTP method.

    You need to override actions() method in TempController.

    You can remove it:

    /**
     * @inheritdoc
     */
    public function actions()
    {
        $defaultActions = parent::actions();
        unset($defaultActions['create']);
        return $defaultActions;
    }
    

    And now you can write your own actionCreate method like you tried before.

    Or you can create separate action class and use it instead:

    /**
     * @inheritdoc
     */
    public function actions()
    {
        $defaultActions = parent::actions();
        $defaultActions['create'] = [
            'class' => 'yiiest\CreateAction', // change it to your class
            // modify configuration below, or not
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
            'scenario' => $this->createScenario,
        ];
        return $defaultActions;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料