Yii2的restfull web服务配置
一.开启服务器的静态化
查看apache配置文件,找到下面代码
LoadModule rewrite_module modules/mod_rewrite.so
将其注释去掉;如果没有找到则添加进去;重启apache服务器。
然后在网站的根目录下面创建一个.htaccess文件,内容如下:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php
</IfModule>
二.创建一个模型
数据库准备一个名字为yii_nav的数据表,通过gii来创建Nav.php模型。
1 <?php 2 3 namespace app\models; 4 5 use Yii; 6 use yii\web\Link; 7 use yii\web\Linkable; 8 use yii\helpers\Url; 9 10 /** 11 * This is the model class for table "yii_nav". 12 * 13 * @property integer $nav_id 14 * @property integer $nav_pid 15 * @property integer $nav_type 16 * @property string $nav_icon 17 * @property integer $nav_sort 18 * @property string $homePage 19 * @property integer $collapsed 20 * @property integer $closeable 21 * @property string $id 22 * @property string $text 23 * @property string $href 24 * @property integer $status 25 */ 26 class Nav extends \yii\db\ActiveRecord implements Linkable 27 { 28 /** 29 * @inheritdoc 30 */ 31 public static function tableName() 32 { 33 return 'yii_nav'; 34 } 35 36 /** 37 * @inheritdoc 38 */ 39 public function rules() 40 { 41 return [ 42 [['nav_pid', 'nav_type', 'nav_sort', 'collapsed', 'closeable', 'status'], 'integer'], 43 [['nav_icon'], 'string', 'max' => 20], 44 [['homePage', 'href'], 'string', 'max' => 100], 45 [['id', 'text'], 'string', 'max' => 50] 46 ]; 47 } 48 49 /** 50 * @inheritdoc 51 */ 52 public function attributeLabels() 53 { 54 return [ 55 'nav_id' => 'Nav ID', 56 'nav_pid' => 'Nav Pid', 57 'nav_type' => 'Nav Type', 58 'nav_icon' => 'Nav Icon', 59 'nav_sort' => 'Nav Sort', 60 'homePage' => 'Home Page', 61 'collapsed' => 'Collapsed', 62 'closeable' => 'Closeable', 63 'id' => 'ID', 64 'text' => 'Text', 65 'href' => 'Href', 66 'status' => 'Status', 67 ]; 68 } 69 70 }
二.创建一个控制器
<?php namespace app\controllers; use yii\rest\ActiveController; class NavController extends ActiveController { public $modelClass = 'app\models\Nav'; }
要注意的是,与普通控制器继承的类不同,此处继承ActiveController类。
三.配置URL规则
1 'urlManager' => [ 2 'enablePrettyUrl' => true, 3 'showScriptName' => false, 4 'enableStrictParsing' => true, 5 'rules' => [ 6 ['class' => 'yii\rest\UrlRule', 'controller' => ['nav']], 7 // '<controller:\w+>/<id:\d+>' => '<controller>/view', 8 // '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', 9 // '<controller:\w+>/<action:\w+>' => '<controller>/<action>', 10 ], 11 ]
注意:将urlManager这段代码添加到components组件里面;enablePrettyUrl 为启用美化URL;showScriptName 在URL路径中是否显示脚本入口文件;enableStrictParsing 为是否执行严格的url解析;注释掉的三行代码,是yii最初版本的配置;对于yii2已经不需要了。也可以将注释去掉,将yii2的新增加的放到前面去,规避没有定义路由的页面的不能正常访问的问题。
四.开启JSON格式输出
1 'request' => [ 2 'parsers' => [ 3 'application/json' => 'yii\web\JsonParser', 4 ] 5 ]
注意:将request这段代码同样添加到components组件里面;
五.操作
特别注意:URLManager中controller定义的为nav,而我们访问时要加一个s字符操作。
GET /navs: 按页列出所有nav中的数据;HEAD /navs: 显示nav列表的概要信息;POST /navs: 创建一个新的nav行;GET /navs/1: 返回第一个nav的详细信息;HEAD /navs/1: 显示第一个nav的概要信息;PATCH /navs/1andPUT /users/123: 更新第一个nav信息; 注意:如果用Postman进行测试的话,contentType要选application/x-www-form-urlencoded操作DELETE /navs/1: 删除第一个nav;OPTIONS /navs: 显示支持的动作/navs;OPTIONS /navs/1: 显示支持的动作/navs/123.

浙公网安备 33010602011771号