dersonsena / yii2-app-restapi
Yii 2 Rest API 项目模板
Requires
- php: >=5.4.0
- yiisoft/yii2: ~2.0.14
- yiisoft/yii2-bootstrap: ~2.0.0
- yiisoft/yii2-swiftmailer: ~2.0.0
Requires (Dev)
- codeception/base: ^2.2.3
- codeception/specify: ~0.4.3
- codeception/verify: ~0.3.1
- yiisoft/yii2-debug: ~2.0.0
- yiisoft/yii2-faker: ~2.0.0
- yiisoft/yii2-gii: ~2.0.0
README
Yii 2 API Rest 项目模板
Yii 2 API Rest 项目模板是一个适用于快速创建 API Rest 项目的最佳 Yii 2 应用程序骨架。
目录结构
目录结构类似于在高级功能中添加的基本模板,包括根据您的环境生成 *-local.php
、web/index.php
文件和 yii
脚本。
+ assets/ contains assets definition
+ components/ containes the application components
+ commands/ contains console commands (controllers)
- config/ contains application configurations
* routes/ contains the routes configurations files
+ environments contains environments templates files (see the advanced template)
+ mail/ contains view files for e-mails
+ migrations/ contains the migrations scripts
+ models/ contains model classes
+ modules/ contains de application modules
+ runtime/ contains files generated during runtime
+ tests/ contains various tests for the basic application
+ vendor/ contains dependent 3rd-party packages
+ web/ contains the entry script and Web resources
需求
本项目模板的最低需求是您的 Web 服务器支持 PHP 5.4.0。
安装
通过 Composer 安装
如果您没有 Composer,您可以按照 getcomposer.org 上的说明进行安装。
然后,您可以使用以下命令安装此项目模板
php composer create-project --stability=dev dersonsena/yii2-app-restapi
现在,您应该可以通过以下 URL 访问应用程序,假设 basic
是 Web 根目录下的目录。
https:///basic/web/
使用 Docker 安装
更新您的供应商包
docker-compose run --rm php composer update --prefer-dist
运行安装触发器(创建 cookie 验证代码)
docker-compose run --rm php composer install
启动容器
docker-compose up -d
然后,您可以通过以下 URL 访问应用程序
http://127.0.0.1:8000
更多信息请参阅: https://hub.docker.com/_/mysql
注意
- 最低要求的 Docker 引擎版本为
17.04
(开发用,请参阅 性能调优卷挂载) - 默认配置使用您家目录中的主机卷
.docker-composer
作为 composer 缓存
配置
环境文件
与高级模板类似,为了根据您的工作环境生成文件,您需要执行 init
脚本,然后选择您的环境: 开发
或 生产
此脚本将创建文件
/yii
/web/index.php
/config/console-local.php
/config/web-local.php
注意
有关使用方法的更多详细信息,请参阅 高级模板文档。
数据库
使用真实数据编辑文件 config/web-local.php
,例如
'components' => [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=127.0.0.1;dbname=your_database_name', 'username' => 'root', 'password' => '', 'charset' => 'utf8', // Schema cache options (for production environment) //'enableSchemaCache' => true, //'schemaCacheDuration' => 60, //'schemaCache' => 'cache', ], ],
注意
- Yii 不会为您创建数据库,您必须在访问它之前手动执行此操作。
- 您还需要在
config/console-local.php
中配置数据库指南,用于 CLI 脚本。
迁移
使用以下命令运行迁移脚本来创建用户表
php ./yii migrate
User.php
模型已经由 GII 生成,但已添加了一些内容。要访问它,只需打开 models/User.php
文件
注意
所有模型 必须 扩展 app\components\ModelBase
类。请参阅它以获取更多实现细节。
API 路由
您的所有 API 路由都必须位于 config/routes
中。以下代码是已配置的用户路由
return [ [ 'class' => 'yii\rest\UrlRule', 'controller' => 'v1/system/users', 'tokens' => ['{id}' => '<id:[\\w-]+>'], 'pluralize' => false, ] ];
访问用户 Web 服务
要通过您的 REST API 访问用户列表,只需在浏览器或外部应用程序(如 Postman)中访问以下地址即可
GET http://<YOUR_BASE_URL>/v1/system/users
访问此地址时,您应该看到如下JSON所示的一个用户列表
[ { "id": 1, "username": "admin", "password_reset_token": null, "email": "admin@admin.com.br", "status": 1, "created_at": "09/04/2018 04:18:52", "updated_at": "09/04/2018 04:18:52" } ]
更多详情请参阅RESTful Web Services Guide。
身份验证
使用的身份验证是HttpBearerAuth,其中通过其API传递具有JWT的Token进行用户身份验证。最初它是禁用的。要启用它非常简单,只需进入app\components\BaseController
类,取消以下行的注释
$behaviors['authenticator'] = [ 'class' => HttpBearerAuth::className(), 'except' => $excepts ]; $behaviors['jsxValidator'] = [ 'class' => JsxValidator::className(), 'except' => $excepts ]; ]
取消这些行的注释并尝试重新运行用户列表时,您应该返回如下消息
{ "name": "Unauthorized", "message": "Your request was made with invalid credentials.", "code": 0, "status": 401, "type": "yii\\web\\UnauthorizedHttpException" }
注意:如果您想某些操作是开放的,即不需要对API进行身份验证,只需在$excepts
数组内添加即可。
有关Yii的REST身份验证的更多详情,请参阅官方框架文档。