3

I am building a test API. I have created a Controller Page which extends from yii\rest\Controller. Actions needs to send a response.

To access actions in this controller, a service_id value needs to be posted. If present I need to evaluate if that service_id exists, if it is active and belongs to the user logged in. If validation fails, I need to send a response.

I am trying to do it using beforeAction(), but the problem is that return data is used to validate if action should continue or not.

So my temporary solution is saving service object in a Class attribute to evaluate it in the action and return response.

class PageController extends Controller
{

    public $service;

    public function beforeAction($action)
    {
        parent::beforeAction($action);

        if (Yii::$app->request->isPost) {

            $data = Yii::$app->request->post();
            $userAccess = new UserAccess();
            $userAccess->load($data);

            $service = $userAccess->getService();
            $this->service = $service;
        }

        return true;
    }

    public function actionConnect()
    {

        $response = null;

        if (empty($this->service)) {
            $response['code'] = 'ERROR';
            $response['message'] = 'Service does not exist';

            return $response;
        }
    }
}

But I can potentially have 20 actions which require this validation, is there a way to return the response from the beforeAction method to avoid repeating code?

Eduardo
  • 1,781
  • 3
  • 26
  • 61

2 Answers2

9

You can setup response in beforeAction() and return false to avoid action call:

public function beforeAction($action) {
    if (Yii::$app->request->isPost) {
        $userAccess = new UserAccess();
        $userAccess->load(Yii::$app->request->post());
        $this->service = $userAccess->getService();

        if (empty($this->service)) {
            $this->asJson([
                'code' => 'ERROR',
                'message' => 'Service does not exist',
            ]);

            return false;
        }
    }

    return parent::beforeAction($action);
}
rob006
  • 21,383
  • 5
  • 53
  • 74
  • If I read your code correctly, $this->asJson is only converting the array to json but it is not sending the response correct?, return false will stop the actions to be triggered which is good. I was thinking in add an action "actionNoService" and add a condition to beforeAction using $action->id == "noService" to avoid evaluating this action – Eduardo Jun 27 '18 at 04:48
  • `asJson()` configure global response object - it will be used as a response if you return `false` in `beforeAction()`. Just try it. – rob006 Jun 27 '18 at 09:29
  • 1
    asJson works but $this->render and $this->renderPartial produce a blank screen. Any idea why? – Nik Dow Apr 24 '20 at 10:57
  • 3
    @NikDow `render()` and `renderPartial()` does not configure response object, just return rendered content. You need to do that manually: `Yii::$app->response->content = $this->render($view, $data);`. – rob006 Apr 24 '20 at 11:32
-1

maybe paste in beforeAction after $this->service = $service;

if (empty($this->service)) {
    echo json_encode(['code' => 'ERROR', 'message' => 'Service does not exist']);
    exit;
}
Lukasz
  • 19
  • 1
  • 4
  • Using `exit` is a really bad idea - it will brake the whole framework flow (events, response processing). – rob006 Jun 24 '18 at 17:33