最新消息: 新版网站上线了!!!

Yii中共用Action的实现方法

先在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的实现方法