'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2basic',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];
将此处的用户名和密码更改为本地对应。
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'player'],
],
],
在components下添加url配置。
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
如下,
Url::to(['user/view', 'id' => $this->id], true),
];
}
}
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"
}
}
}
]
{
"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作为主键发现问题依然存在。