Yii2.0 实现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
'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => true, 'showScriptName' => false, 'rules' => [ ['class' => 'yii\rest\UrlRule', 'controller' => 'player'], ], ],在components下添加url配置。
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}" ] }报错提示调用了不存在的方法,由Yii2.0 初识 RESTful Serializer可知此处是一个对模型序列化的方法,返回到Controller的声明中可以发现确实初始化了$serializer变量。可以看出:
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; } }应当是此处传入的$data并没有serializeModel()方法,查找serializeModel()的定义,发现app/rest/Serializer::serializeModel();在此处有一个定义,可见传入的数据并不一定会有serializeModel方法,除非传入的数据已经是一个app/rest/Serializer的实例,那么注释掉$data->serializeModel()这个条件,进行测试,这次并没有报错,但是返回了一个空的结果集。
-------------------------------------------第一次更新----------------------------------------
这次我对数据库中没有的记录进行了查询,这次返回的结果给了很大的提示:
{ "name": "Not Found", "message": "Object not found: 3", "code": 0, "status": 404, "type": "yii\\web\\NotFoundHttpException" }确实数据库中并没有主键为3的这条记录,所以在数据库更换主键再进行测试,这次以username作为主键发现问题依然存在。
文章来自:http://blog.csdn.net/alanjager/article/details/51360604