2

我正在使用带有动作控制器的 yii 框架,控制器中的每个动作都发出相同的请求,但有一些细微的变化。我正在使用我的控制器来调用 API(我制作的扩展)。而且我想知道如何自动设置控制器以调用 API,但具有适合该控制器的特定值和属性。所以我不需要将每个代码复制粘贴到所有控制器。

我想知道如何使用 accesssControl 过滤器做同样的事情(配置一个类/函数和所有控制器组件的所有规则)。

例子:

      <?php
    public function actionLoadDataUser()
{


            //Set model
    $model = new User_DataForm;

    $function = "LoadDataUser";           //Set Method to set or get from (API)

            //Set model name
            $model_name = get_class($model);

    // collect user input data
    if(isset($_POST[$model_name]))
    {       

                    $model->attributes = $_POST[$model_name];
                                        $response = Yii::app()-API->SendRawData($function,  $model->attributes);    // server and get response by function and data
                switch ($response)
                {
        case 'OK':
            Yii::app()->user->setFlash('info','OK');
            $this->refresh();
        break;
        default: 
            Yii::app()->user->setFlash('info','Error!');
            $this->refresh();
                }
    }

    // display the page
    $this->render('index', array('model'=>$model));
}




    public function actionLoadDataAdmin()
{

            //Set model
    $model = new Admin_DataForm;

    $function = "LoadDataAdmin";           //Set Method to set or get from (API)

            //Set model name
            $model_name = get_class($model);

    // collect user input data
    if(isset($_POST[$model_name]))
    {       

                    $model->attributes = $_POST[$model_name];
                    $response = Yii::app()-API->SendRawData($function,  $model->attributes);    // server and get response by function and data
                switch ($response)
                {
        case 'OK':
            Yii::app()->user->setFlash('info','OK');
            $this->refresh();
        break;
                    case 'NOT_ADMIN':
            Yii::app()->user->setFlash('info','Access Denied');
            $this->refresh();
        break;
        default: 
            Yii::app()->user->setFlash('info','Error!');
            $this->refresh();
                }
    }

    // display the page
    $this->render('admin', array('model'=>$model));
}
    ?>
4

1 回答 1

1

编辑:使用一个单独的操作来执行 API 调用,具体取决于您传递给它的参数。

public function actionLoadData()
{
    $modelName = $_POST['model'];
    $model     = new $modelName;
    $function  = $_POST['function'];

    if(isset($_POST[$modelName]))
    {
        $model->attributes = $_POST[$modelName];
        $response = Yii::app()->API->SendRawData($function,  $model->attributes);
        switch ($response)
        {
            case 'OK':
                Yii::app()->user->setFlash('info', 'OK');
                $this->refresh();
                break;
            default: 
                Yii::app()->user->setFlash('info', 'Error!');
                $this->refresh();
        }
    }

    $this->render($_POST['view'], array('model' => $model));
}

请注意,此代码不安全。确保添加一些验证。

于 2012-05-17T08:44:56.763 回答