配置gii
config/main-local.php
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
创建module
因为api接口会随着开发迭代升级,所以我们创建module时的命名空间增加v1,v2类似的表识,通过gii创建module
浏览器输入http://local.yii.com/gii 我本地搭建的项目域名为(local.yii.com),并且我已经做好了url美化的配置
至此,一个 module就生成了
配置 module
config/main.php 注意要配置到components 外层
'modules' => [
'module' => [ //module 与gii生成module配置的ModuleID 一致
'class' => 'app\module\v1\Module',
],
],
到此一个完整module就建立好了 http://域名/module/default/index 即可访问刚刚创建好的module了,如果要建立多个module ,重复上面的步骤。
配置 RESTful api
为了实现restful风格的api,在yii2中,我们需要对控制器进行一下改写
modules/v1/controllers/DefaultController.php
namespace app\module\v1\controllers;
use yii\rest\ActiveController; //restful 风格的api需要控制器继承ActiveController
#use yii\web\Controller;
class DefaultController extends ActiveController
{
/**
* Renders the test view for the module
* @return string
*/
public $modelClass = 'app\models\User'; //控制器必须指定一个model
public function actionTest()
{
echo "test";
}
}
配置路由规则
config/main.php
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => require(__DIR__ . '/rule_v1.php'),
],
#rule_v1.php
return [
[
'class' => 'yii\rest\UrlRule', 'controller' => 'v1/default',
//'except' => ['delete', 'create', 'update'],
'pluralize'=>false,//是否启用复数
'extraPatterns' => [
'POST test' =>'test', //指定只能POST访问
]
],
];
配置 控制器命名空间 与返回结果的数据格式(json)
config/main.php
'controllerNamespace' => 'app\module\v1\controllers',
'request' => [
'csrfParam' => '_csrf-frontend-app',
'parsers' => [ //返回的数据格式为json
'application/json' => 'yii\web\JsonParser',
],
],
通过接口调试工具postman 访问接口
注意RESTful api只能通过接口工具调试,浏览器直接访问会报错
至此,Modules 配置 RESTful api 使用已经完成了,模块化的api就实现了
注意
如果你的控制使用了index,view,create,update,delete,options的方法,可能会出现问题的,因为这些方法是父类(ActiveController)默认的方法,也就是说Yii RESTful api 自带的方法,如果你一定要使用跟这些同名的方法,先重写ActiveController 的actions,verbs函数,避免导致一些奇怪问题
ActiveController.php
public function actions()
{
return [
'index' => [
'class' => 'yii\rest\IndexAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
],
'view' => [
'class' => 'yii\rest\ViewAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
],
'create' => [
'class' => 'yii\rest\CreateAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
'scenario' => $this->createScenario,
],
'update' => [
'class' => 'yii\rest\UpdateAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
'scenario' => $this->updateScenario,
],
'delete' => [
'class' => 'yii\rest\DeleteAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
],
'options' => [
'class' => 'yii\rest\OptionsAction',
],
];
}
protected function verbs()
{
return [
'index' => ['GET', 'HEAD'],
'view' => ['GET', 'HEAD'],
'create' => ['POST'],
'update' => ['PUT', 'PATCH'],
'delete' => ['DELETE'],
];
}