Saulo Lira
Saulo Lira

Reputation: 25

Yii2 redirecting postman request to a different route in the api rest

I am accessing a route by the postman in an api made in Yii2, but the code that I insert in the action that corresponds to that route is not working. Follow the request print:

postman-request-print

The request return was not meant to be the one in the image, because the code I put in the 'create' action was this:

    <?php

    namespace app\modules\api\controllers;

    use Yii;
    use app\modules\pesquisa_mercado\models\PontoDaPesquisa;

    class PesquisaPontoController extends \yii\rest\ActiveController
    {
       public $modelClass = 'app\modules\pesquisa_mercado\models\PesquisaPonto';

    public function behaviors()
    {
        $behaviors = parent::behaviors();

        return $behaviors + [
            [
                'class' => \yii\filters\auth\HttpBearerAuth::className(),
                'except' => ['options'],
            ],
            'verbs' => [
                'class' => \app\modules\api\behaviors\Verbcheck::className(),
                'actions' => [
                    'index' => ['GET'],
                    'create' => ['POST'],
                    'update' => ['PUT'],
                    'view' => ['GET'],
                    'delete' => ['DELETE'],
                    'finalizar-pesquisa' => ['POST'],
                ],
            ],
        ];
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['update']);
        return $actions;
    }

    public function actionCreate()
    {
       die("Test"); // test inserted here
    }

  }

That is, the return was to be 'Test'. For some reason I don't know, the route is being redirected to another place.

I also discovered that the request goes through the getLinks () method present in the PesquisaPonto model:

<?php

namespace app\modules\pesquisa_mercado\models;

class PesquisaPonto extends \yii\db\ActiveRecord implements \yii\web\Linkable
{
  /**
   * @inheritdoc
   */
   public static function tableName()
   {
      return '{{%pesquisa_ponto}}';
   }

     /**
     * @inheritdoc
     */
    public function getLinks()  // the requisition also passes through here!
    {
        return [
            Link::REL_SELF => Url::to(['pesquisa-ponto/view', 'id' => $this->id], true),
            'index' => Url::to(['pesquisa-ponto'], true)
        ];
    }

}

Also follow the configuration of urlManager:

'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => true,
            'rules' => [
                // Pontos de Pesquisa
                // api/pesquisa-ponto
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => [
                        'api/pesquisa-ponto'
                    ],
                    'pluralize' => false,
                ],
             ],
          ]

I still haven't found the reason why Yii2 is redirecting the route and not allowing the postman to access the 'create' action...

Upvotes: 0

Views: 527

Answers (1)

Michal Hynčica
Michal Hynčica

Reputation: 6144

The actions() method in yii\rest\ActiveController looks like this

 public function actions()
 {
    return [
        // ...
        'create' => [
            'class' => 'yii\rest\CreateAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
            'scenario' => $this->createScenario,
        ],
        // ...
    ];
}

In your implementation of actions() method you are only removing the configuration for update action but the configuration for create action is left untouched. That means that the action is run from the yii\rest\CreateAction not the actionCreate() method of the controller.

To run the action from the PesquisaPontoController::actionCreate() you have to unset the configuration for the create action as well.

public function actions()
{
     $actions = parent::actions();
     unset($actions['update'], $actions['create']);
     return $actions;
}

Upvotes: 1

Related Questions