yii restful api
一、创建数据库
首先,在mysql中创建一个名为yii2basic的数据库,并创建一张名为player的表。
二、配置
1.app/config/db.php
- <?php
- return [
- 'class' => 'yii\db\Connection',
- 'dsn' => 'mysql:host=localhost;dbname=yii2basic',
- 'username' => 'root',
- 'password' => '',
- 'charset' => 'utf8',
- ];
2.app/config/web.php
在components下添加url配置。
- 'urlManager' => [
- 'enablePrettyUrl' => true,
- 'enableStrictParsing' => true,
- 'showScriptName' => false,
- 'rules' => [
- ['class' => 'yii\rest\UrlRule', 'controller' => 'player'],
- ],
- ],
3.创建.htaccess
在Yii项目下创建.htaccess,在app/web下创建,其内容为:
- Options +FollowSymLinks
- IndexIgnore /
- RewriteEngine on
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteRule . index.php
三、创建一个控制器
首先,创建一个控制器类 app\controllers\PlayerController
如下,
- namespace app\controllers;
- use yii\rest\ActiveController;
- class UserController extends ActiveController
- {
- public $modelClass = 'app\models\Player';
- public $serializer = 'yii\rest\Serializer';
- public function checkAccess()
- {
- }
- }
控制器类扩展自 yii\rest\ActiveController。通过指定 yii\rest\ActiveController::modelClass 作为 app\models\player
, 控制器就能知道使用哪个模型去获取和处理数据。
四、创建一个模型类
然后,创建一个模型类 app\model\Player
如下,
- <?php
- namespace app\models;
- use yii\db\ActiveRecord;
- use yii\web\Link;
- use yii\web\Linkable;
- use yii\helpers\Url;
- class Player extends ActiveRecord implements Linkable
- {
- public function fields()
- {
- return [
- // 字段名和属性名相同
- 'id',
- // 字段名为"email", 对应的属性名为"email_address"
- 'username',
- 'password',
- ];
- }
- public function getLinks()
- {
- return [
- Link::REL_SELF => Url::to(['user/view', 'id' => $this->id], true),
- ];
- }
- }
到此根据官方文档可知已经生成了对应的一系列API,格式参照Yii2.0
RESTful Web服务(1)
通过命令:
- curl -i -H "Accept:application/json" "http://localhost/YourAppName/web/players"
- [
- {
- "id": 0,
- "username": "",
- "password": "",
- "_links": {
- "self": {
- "href": "http://localhost/first-Yii/web/player/view?id=0"
- }
- }
- },
- {
- "id": 1,
- "username": "dffdfdfd",
- "password": "sdsadsa",
- "_links": {
- "self": {
- "href": "http://localhost/first-Yii/web/player/view?id=1<span style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Hiragino Sans GB', 'Hiragino Sans GB W3', 'WenQuanYi Micro Hei', 'Microsoft YaHei UI', 'Microsoft YaHei', sans-serif; white-space: pre-wrap;">"</span>
- }
- }
- }
- ]
使用Postman对功能进行测试发现,CURD操作中的CD操作行为正常,但是请求单条记录的操作的则会返回如下的错误:
- {
- "name": "Exception",
- "message": "Calling unknown method: app\\models\\Player::serializeModel()",
- "code": 0,
- "type": "yii\\base\\UnknownMethodException",
- "file": "/Applications/XAMPP/xamppfiles/htdocs/first-Yii/vendor/yiisoft/yii2/base/Component.php",
- "line": 285,
- "stack-trace": [
- "#0 /Applications/XAMPP/xamppfiles/htdocs/first-Yii/vendor/yiisoft/yii2/rest/Serializer.php(137): yii\\base\\Component->__call('serializeModel', Array)",
- "#1 /Applications/XAMPP/xamppfiles/htdocs/first-Yii/vendor/yiisoft/yii2/rest/Serializer.php(137): app\\models\\Player->serializeModel()",
- "#2 /Applications/XAMPP/xamppfiles/htdocs/first-Yii/vendor/yiisoft/yii2/rest/Controller.php(97): yii\\rest\\Serializer->serialize(Object(app\\models\\Player))",
- "#3 /Applications/XAMPP/xamppfiles/htdocs/first-Yii/vendor/yiisoft/yii2/rest/Controller.php(75): yii\\rest\\Controller->serializeData(Object(app\\models\\Player))",
- "#4 /Applications/XAMPP/xamppfiles/htdocs/first-Yii/vendor/yiisoft/yii2/base/Controller.php(156): yii\\rest\\Controller->afterAction(Object(yii\\rest\\ViewAction), Object(app\\models\\Player))",
- "#5 /Applications/XAMPP/xamppfiles/htdocs/first-Yii/vendor/yiisoft/yii2/base/Module.php(454): yii\\base\\Controller->runAction('view', Array)",
- "#6 /Applications/XAMPP/xamppfiles/htdocs/first-Yii/vendor/yiisoft/yii2/web/Application.php(84): yii\\base\\Module->runAction('player/view', Array)",
- "#7 /Applications/XAMPP/xamppfiles/htdocs/first-Yii/vendor/yiisoft/yii2/base/Application.php(375): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
- "#8 /Applications/XAMPP/xamppfiles/htdocs/first-Yii/web/index.php(12): yii\\base\\Application->run()",
- "#9 {main}"
- ]
- }
- public function serialize($data)
- {
- if ($data instanceof Model && $data->serializeModel()) {
- return $this->serializeModelErrors($data);
- } elseif ($data instanceof Arrayable) {
- return $this->serializeModel($data);
- } elseif ($data instanceof DataProviderInterface) {
- return $this->serializeDataProvider($data);
- } else {
- return $data;
- }
- }
-------------------------------------------第一次更新----------------------------------------
这次我对数据库中没有的记录进行了查询,这次返回的结果给了很大的提示:
- {
- "name": "Not Found",
- "message": "Object not found: 3",
- "code": 0,
- "status": 404,
- "type": "yii\\web\\NotFoundHttpException"
- }