yii restful api
阿新 • • 發佈:2018-01-23
模型 tty ocs calling nag pfile script dsa har
3.創建.htaccess 在Yii項目下創建.htaccess,在app/web下創建,其內容為:
如下,
然後,創建一個模型類
使用Postman對功能進行測試發現,CURD操作中的CD操作行為正常,但是請求單條記錄的操作的則會返回如下的錯誤:
一、創建數據庫
首先,在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‘,
- ];
- ‘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"
- }
yii restful api