pestak
pestak

Reputation: 1

Yii2 REST other actions

I have a controller called DepartementsController which extends the yii\rest\ActiveController. I have to know which are the department in my region. When I test my action http://localhost/yii_2/ws_localisation/web/regions/01/departements I have this error :

{
"name": "PHP Notice",
"message": "Trying to get property of non-object",
"code": 8,
"type": "yii\base\ErrorException",
"file": "E:\wamp\www\yii_2\ws_localisation\controllers\DepartementsController.php",
"line": 58,
"stack-trace": [
"#0      E:\wamp\www\yii_2\ws_localisation\controllers\DepartementsController.php(58): yii\base\ErrorHandler->handleError(8, 'Trying to get p...', 'E:\\wam...', 58, Array)",
"#1 [internal function]: app\controllers\DepartementsController->actionDepartements('01')",
"#2 E:\Laetitia\wamp\www\yii_2\ws_localisation\vendor\yiisoft\yii2\base\InlineAction.php(55): call_user_func_array(Array, Array)",
"#3 E:\Laetitia\wamp\www\yii_2\ws_localisation\vendor\yiisoft\yii2\base\Controller.php(154): yii\base\InlineAction->runWithParams(Array)",
"#4 E:\Laetitia\wamp\www\yii_2\ws_localisation\vendor\yiisoft\yii2\base\Module.php(454): yii\base\Controller->runAction('departements', Array)",
"#5 E:\Laetitia\wamp\www\yii_2\ws_localisation\vendor\yiisoft\yii2\web\Application.php(84): yii\base\Module->runAction('departements/de...', Array)",
"#6 E:\Laetitia\wamp\www\yii_2\ws_localisation\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))",
"#7 E:\Laetitia\wamp\www\yii_2\ws_localisation\web\index.php(12): yii\base\Application->run()",
"#8 {main}"
]
}

Can you help me please. Regards.

web.php

'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => [                
            [
                'class' => 'yii\rest\UrlRule',
                'controller' => ['departements'],
                'prefix' => '/regions/<id:\\w+>',
                'patterns' => [
                    'GET,HEAD' => 'departements', 
                ],
            ],                
            [
                'class' => 'yii\rest\UrlRule',
                'controller' => ['regions','departements','communes','sylvoecoregions'],
                'tokens' => ['{id}' => '<id:\\w+>'],
                'except' => ['create','update','delete'],
            ],
        ],
    ], 

DepartementsController.php

namespace app\controllers;

use yii\rest\ActiveController;
use yii\db\ActiveRecord;
use yii\web\ServerErrorHttpException;

class DepartementsController extends ActiveController
{
public $modelClass = 'app\models\Departements';

public function behaviors()
{
    return 
    \yii\helpers\ArrayHelper::merge(parent::behaviors(), [
        'corsFilter' => [
            'class' => \yii\filters\Cors::className(),
        ],
    ]);
}

public function actionDepartements($id)
{
    $model = new $this->modelClass;
    $model_departement = $model->find()->where('region_id=:id',[':id'=>$id])->all();

    if($model_departement)
    {
        //$this->setHeader(200);
        echo json_encode(array('status'=>1,'data'=>array_filter($model_departement->attributes)),JSON_PRETTY_PRINT);
    }
    else
    {
        //$this->setHeader(400);
        echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
    }
}}

Upvotes: 0

Views: 158

Answers (1)

FZE
FZE

Reputation: 1627

If do you know what class that $model_departement is an instance

just change your if condition as

`if ($model_departement instanceof ModelDepartmentClass) {`

But if you have no idea about that. Below line also works.

if (is_object($model_departement)) {

if ($model_departement) is not a qualified check.

Please check related documents for more information.

http://php.net/manual/tr/function.is-object.php

http://php.net/manual/en/language.operators.type.php

http://php.net/manual/en/language.expressions.php

Upvotes: 0

Related Questions