API RESTful Doubt


(Foruns) #1

Hello

I’m studyng API RESTful, and I’m using ActiveController.

The basics elements are working ok (index, create, update, etc).

So I created my own action "test". I want that my action work only post method. How can I do it? I use POSTMAN app to teste. If I send GET the action test return resut, if I send POST too. I need that only POST method accept request.

This is my code:




<?php


namespace app\modules\api\controllers;


use Yii;

use yii\rest\ActiveController;

use yii\filters\auth\CompositeAuth;

use yii\filters\auth\HttpBasicAuth;

use yii\filters\auth\HttpBearerAuth;

use yii\filters\auth\QueryParamAuth;

use app\models\Test;


class DefaultController extends ActiveController

{

    //Define que para as requisições que o controller receber será utilizado

    //como base o model alunos para recuperação e configuração de dados.

    public $modelClass = 'app\models\Aluno';




    public function behaviors()

    {

        $behaviors = parent::behaviors();

        $behaviors['authenticator'] = [

            'class' => CompositeAuth::className(),

            'authMethods' => [

                //HttpBasicAuth::className(),

               // HttpBearerAuth::className(),

                QueryParamAuth::className(),

            ],

        ];

        retclass DefaultController extends ActiveController

{

    //Define que para as requisições que o controller receber será utilizado

    //como base o model alunos para recuperação e configuração de dados.

    public $modelClass = 'app\models\Aluno';




    public function behaviors()

    {

        $behaviors = parent::behaviors();

        $behaviors['authenticator'] = [

            'class' => CompositeAuth::className(),

            'authMethods' => [

                //HttpBasicAuth::className(),

               // HttpBearerAuth::className(),

                QueryParamAuth::className(),

            ],

        ];

        return $behaviors;

    }


public function beforeAction($action)

    {

        return parent::beforeAction($action);

    }


    //Não permite que o usuário delete e nem faça criações via API.

    public function actions()

    {

        //Recupera as actions da classe pai.

        $actions = parent::actions();


        //Impede requisições delete e create via API.

        //unset($actions['delete'],$actions['create']);

        //unset($actions['index']);


        //Retorna as actions modificadas.

        return $actions;

    }


public function actionTestlist(){

        header('Content-type: application/json; charset=utf-8');

        $array = [];


       if(isset($_POST)){

            $test = Testt::find()->where('id>0')->all();


            foreach ($testt as $item) {

                $array[] = ['token'=>$item->nome]  ;

            }

       }

       else{

            $array = ["erro"=>"erro"];

       }

        echo json_encode($array);


    }




The line:

if(isset($_POST)){…}

Dont work.


(Jacob Moen) #2

[color="#006400"]/* moved to REST (no pun intented) */[/color]


(Andrzej Bernat) #3

Use verb filter http://www.yiiframework.com/doc-2.0/yii-filters-verbfilter.html