1、在项目下添加目录weChatApi
2、在common/config/bootstrap.php
文件添加一行代码:
Yii::setAlias( '@weChatApi' , dirname( dirname( __DIR__ ) ) . '/weChatApi' );
3、在weChatApi
里面创建如下目录:
4、修改/etc/nginx/conf.d/default.conf
添加如下代码:
location /BlackCard/weChatApi/web/ {
index index.html index.htm index.php;
try_files $uri $uri/ /BlackCard/weChatApi/web/index.php?$query_string;
}
重启nginx服务:systemctl reload nginx
5、在weChatApi
添加目录:runtime
,命令如下:
mkdir runtime
chmod 777 runtime
6、各个文件的代码如下:
ApiController.php
:
<?php
namespace weChatApi\base\controllers;
use yii\filters\auth\HttpBasicAuth;
use yii\rest\ActiveController;
use Yii;
/**
* restful api 底层控制器
* Class ApiController
* @package weChatApi\base\controllers
*/
class ApiController extends ActiveController
{
/**
* 操作成功标示码
*/
const ACTION_SUCCESS_CODE = 1;
/**
* 操作失败标示码
*/
const ACTION_FAIL_CODE = 0;
/**
* 开发的权限
* @var array
*/
private $allowedApis = [
'user/login' ,
];
/**
* 关闭csrf验证
* @var bool
*/
public $enableCsrfValidation = false;
/**
* 验证授权
*
* @param array $optional
*
* @return array
*/
public function behaviors( $optional = [] )
{
$behaviors = parent::behaviors();
$behaviors[ 'authenticator' ] = [
'class' => HttpBasicAuth::className() ,
];
return $behaviors;
}
/**
* 重写
*
* @param $action
*
* @return bool
* @throws \yii\web\BadRequestHttpException
*/
public function beforeAction( $action )
{
if ( $this->isAllowedApi() ) {
return true;
}
parent::beforeAction( $action );
return true;
}
/**
* 注销自带的 rest
* @return array
*/
public function actions()
{
$actions = parent::actions();
unset(
$actions[ 'index' ] ,
$actions[ 'updat