user3312884
user3312884

Reputation: 97

yii2 custom REST api issue

I have tried all the way to make these run but no use.There is lots of problem and confusion i m going through. I have make api which return all the countries which is working fine.Now need to write api function to list all the states of perticular country.

api : http://phpserver:8090/ssn-project/newzit/api/web/state/customstate?country_id=102

StateController.php

class StateController extends ActiveController{

public $modelClass = 'api\modules\state\models\State'; 

public function actionCustomState($country_id)
{
    $model = new $this->modelClass;
    $result = $model::find()
              ->where(['country_id' => $country_id])
              ->all();
    return $result;
}  
}

main.php

'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => [
            [
                'class' => 'yii\rest\UrlRule', 
                'controller' => ['country/country','state/state','category/category','seller/seller'],
                'extraPatterns' => [
                    'GET CustomState' => 'CustomState',
                ],  
            ]
        ],        
    ]

Am I doing anything wrong.Please help

Upvotes: 1

Views: 141

Answers (2)

user3312884
user3312884

Reputation: 97

Found the solution. made 'pluralize'=>false and used custom-state in url My main.php

'rules' => [
            [
                'pluralize'=>false,
                'class' => 'yii\rest\UrlRule', 
                'controller' => ['country/country','state/state','category/category','seller/seller','contactus/contactus'],
                'extraPatterns' => [
                    'GET custom-state' => 'custom-state',
                ],  

            ]
        ],   

Thank you.

Upvotes: 1

Andrew
Andrew

Reputation: 1866

What do you mean by 'controller' => ['country/country','state/state','category/category','seller/seller'] ? This will be treated as module/controller. You have placed all controllers inside different modules? With this logic, your api url will be

http://phpserver:8090/ssn-project/newzit/api/web/state/state/customstate?country_id=102

instead of

http://phpserver:8090/ssn-project/newzit/api/web/state/customstate?country_id=102

Upvotes: 1

Related Questions