yii2自定义REST api问题

时间:2016-04-18 07:42:13

标签: api yii2

我一直试图让这些运行但没有用。我遇到了很多问题和困惑。 我已经使api返回所有工作正常的国家。现在需要编写api函数来列出特定国家的所有状态。

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',
                ],  
            ]
        ],        
    ]

我做错了什么。请帮忙

2 个答案:

答案 0 :(得分:1)

'controller' => ['country/country','state/state','category/category','seller/seller']你是什么意思?这将被视为模块/控制器。您已将所有控制器放在不同的模块中?有了这个逻辑,你的api url就会

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

而不是

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

答案 1 :(得分:1)

找到解决方案。 make'复数' => false并在url中使用自定义状态 我的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',
                ],  

            ]
        ],   

谢谢。