Dar
Dar

Reputation: 401

Creating consistant controllers on YII framework

I'm using yii framework with action controller that every action in the controller make the same request but with minors changes. I'm using my controllers to call API (extension that I made). And I would like to know how can I automatically set controllers to call API but with the specific values and attributes that is right for this controller. So I wouldn't need to copy paste every code to all controllers.

I would like to know how to do the same thing with accesssControl filter (Config all rules I one class/function and all controller components).

Example:

      <?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));
}
    ?>

Upvotes: 2

Views: 922

Answers (1)

Samy Dindane
Samy Dindane

Reputation: 18706

Edit: use one single action that will do your API calls depending of the parameters you pass to it.

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));
}

Note that this code isn't secure. Make sure you add some validations.

Upvotes: 1

Related Questions