PHP7中文手册2018 带注释 最新chm版
先在protected\components中建立DeleteAction.php,内容如下:
class DeleteAction extends CAction { public $pk = 'id'; public $redirectTo = 'index'; public $modelClass; function run() { if(empty($_GET[$this->pk])) throw new CHttpException(404); $model = CActiveRecord::model($this->modelClass)->findByPk($_GET[$this->pk]); if(!$model) throw new CHttpException(404); if($model->delete()) $this->redirect($this->redirectTo); throw new CHttpException(500); } } 在要用到此Action的地方加上:function actions() { return array( 'delete' => array( 'class' => 'DeleteAction', 'modelClass' => 'User',//model名字 ); ); }
转载请注明:谷谷点程序 » Yii中共用Action的实现方法