1
votes

i Have a question for you guys :) I'm trying to add an API on my yii2 advanced template as i want my wordpress website send datas to my yii2 app. My system : 1) Yii2 advanced template 2) wordpress website 3) my wordpress plugin with vuejs and axios to create a new entry in my yii2 app via API So what i allready did : common/config/main.php (as i use AccessController, i added orders/* to allow it)

'as access' => [
'class' => 'mdm\admin\components\AccessControl',
'allowActions' => [
    'orders/*',
],

frontend/config/main.php

'components' => [
'request' => [
    'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ],

and (in urlManager array)

['class'=>'yii\rest\UrlRule','controller'=>'Orders'],
[‘class'=>'yii\rest\UrlRule','controller'=>'Contacts']

Then my controller :

<?php
namespace frontend\controllers;
use yii\rest\ActiveController; use yii\filters\auth\HttpBasicAuth;
class OrdersController extends ActiveController {
public $modelClass = 'common\models\Vctorders';

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

// remove authentication filter
$auth = $behaviors['authenticator'];
unset($behaviors['authenticator']);

// add CORS filter
$behaviors['corsFilter'] = [
    'class' => \yii\filters\Cors::className(),
];

// re-add authentication filter
$behaviors['authenticator'] = $auth;
// avoid authentication on CORS-pre-flight requests (HTTP OPTIONS method)
$behaviors['authenticator']['except'] = ['options'];

return $behaviors;
}

public function actions()
{
$actions = parent::actions();
unset($actions['create']);
unset($actions['update']);
unset($actions['delete']);
unset($actions['view']);
//unset($actions['index']);
return $actions;
}

protected function verbs(){
return [
    'create' => ['POST'],
    'new' => ['POST'],
    'update' => ['PUT', 'PATCH','POST'],
    'delete' => ['DELETE'],
    'view' => ['GET'],
    //'index'=>['GET'],
];
}


public function actionCreate()
{
    $model = new Vctorders();

    $model->date_creation = date('Y-m-d H:i:s',strtotime('now'));
    $model->etat = 0;
    if($model->save()){ 
      return 'OK';

   } else{
      return 'error';
   }


}


}

So, i a use Postman with : http://localhost:8888/SD/sdms/orders/ i get a record, no problem

But when i do a POST with :

http://localhost:8888/SD/sdms/orders/create?livre=L'Arbre Musicien&langue=Allemand&nom=Perroud&prenom=LIttledave&nombre=2&npa=1221&pays=suisse&accept_pc=1&[email protected]&etat=1&message=lbablalbal&tel=01201201212

the answer is

{"name":"Exception","message":"Class 'frontend\\controllers\\Vctorders' not found","code":0,"type":"Error","file":"/Applications/MAMP/htdocs/SD/sdms/frontend/controllers/OrdersController.php","line":58,"stack-trace":["#0 [internal function]: frontend\\controllers\\OrdersController->actionCreate()","#1 /Applications/MAMP/htdocs/SD/sdms/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)","#2 /Applications/MAMP/htdocs/SD/sdms/vendor/yiisoft/yii2/base/Controller.php(157): yii\\base\\InlineAction->runWithParams(Array)","#3 /Applications/MAMP/htdocs/SD/sdms/vendor/yiisoft/yii2/base/Module.php(528): yii\\base\\Controller->runAction('create', Array)","#4 /Applications/MAMP/htdocs/SD/sdms/vendor/yiisoft/yii2/web/Application.php(103): yii\\base\\Module->runAction('orders/create', Array)","#5 /Applications/MAMP/htdocs/SD/sdms/vendor/yiisoft/yii2/base/Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))","#6 /Applications/MAMP/htdocs/SD/sdms/frontend/web/index.php(17): yii\\base\\Application->run()","#7 {main}"]}
1

1 Answers

0
votes

The problem is you are sending your request in POSTMAN as a GET REQUEST, and your ACTION in the CONTROLLER expects a POST REQUEST.

In your POSTMAN client at the left side, there is a selector where you can choose what kind of request you are doing.

The second error you get is because once you remove the requisite of POST, the GET request enters the action and goes here:

} elseif (!\Yii::$app->request->isPost) {
   $model->load(Yii::$app->request->get());
}

And tries to load the model with the data in the GET parameters, but fails as there is no way the default $model->load() knows how to map the data in your GET petition.

In any case (GET or POST) the $model->load() will not work, as if you check the load() function you will find that it searches for the Object modal name inside the array to load the parameters, so you must do like:

http://localhost:8888/SD/sdms/orders/create?Orders%5Blivre%5=L'Arbre

But for each parameter, the strange characters you see in the result of stringify ['Orders' => ['livre' => 'Larbre']].