一、apache配置
找到httpd.conf中的这两段内容:
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride none
将AllowOverride none</code> 改为 <code>AllowOverride all
#LoadModule rewrite_module libexec/apache2/mod_rewrite.so
将#去掉,打开rewrite模块
二、.htaccess文件
写入下面内容,并放入yii2根目录的web目录下:webroot/yii2/web(yii2是自己命名的项目名称,webroot的Apache根目录)
Options +FollowSymLinks
IndexIgnore /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
三、config/web.php配置urlManager:
.....
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule', 'controller' => 'user' ,
],
],
] ,
访问路径:http://loacalhost/yii2/web/users</code>(yii自动讲名词变为复数形式,具体看文档,可取消)
四、controller继承ActiveController:
namespace app\controllers;
use yii\rest\ActiveController;
class CountryController extends ActiveController
{
public $modelClass = 'app\models\Country';
...
}
额外、不同modules下urlmanger配置:
假设生成了一个modules/v1 的模块,urlmanger配置如下
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule', 'controller' => ['v1/country'] ,
],
],
] ,
访问路径:http://loacalhost/yii2/web/v1/countries(yii自动讲名词变为复数形式,具体看文档,可取消)
秋水
{"name":"路人黄"}
廉贞
Ksana
qmore
Ksana
qmore
贝克汉狼
iathanasy .
往昔
HI
忘了时间的钟