搭建新的目录结构详细点击这里
搭建Restful API 点击这里
1.开发环境
操作系统 Windows Server 2012 R2 Datacenter
PHP架构 Apache版UPUPW PHP5.4
编程软件 Zend Studio 12.0.2
2.环境配置
解压缩UPUPW到D盘根目录,分别在UPUPW中新添加三个站点,如下
http://admin.mxq.com
http://www.mxq.com
http://api.mxq.com
网站目录都是用默认路径,然后修改D:\upupw\Apache2\conf\httpd-vhosts.conf文件,例如
将http://admin.mxq.com的DocumentRoot 修改为D:/upupw/vhosts/mxq.com/backend/web,php_admin_value open_basedir修改为D:\upupw\vhosts\mxq.com\,Directory修改为D:/upupw/vhosts/mxq.com/backend/web,其他站点也做相应修改
3.通过归档文件安装 Yii
- 3.1 从 yiiframework.com 下载 Advanced 归档文件。将下载的文件解压缩到 D:\upupw\vhosts\mxq.com 目录中。
- 3.2 把php.exe加入系统环境变量
- ① 右击我的电脑-属性-高级-环境变量
- ②找到 Path 这一项(可能需要向下滚动才能找到),鼠标双击 Path 这一项,在最后加入你的 PHP 目录和类库所在的路径,包括前面的“;”(例如:;C:\php;C:\php\ext)
- ③点击“新建”按钮并在“变量名”中输入“PHPRC”,在“变量值”中输入 php.ini 文件所在的目录(例如:C:\php) ,这个步骤是为了让windows找到php.ini.
- 3.3 运行CMD 进入安装目录中,执行init或在安装目录中运行init.bat,选择 0 开发模式进行安装
- 3.4在UPUPW中添加本地域名解析,这样就可以通过http://admin.mxq.com进行访问,其他两个类似。
4.添加新应用API
新部署的YII只有backend和frontend,我们手动添加api应用
- 4.1 复制frontend下的所有文件到api文件夹,批量替换frontend为api
- 4.2 打开D:\upupw\vhosts\mxq.com\common\config\bootstrap.php,复制包含frontend的那一行,并将frontend改为api,为应用增加别名。
- 4.3 打开D:\upupw\vhosts\mxq.com\environments\index.php,复制包含frontend的行,改为api,Development和Production下的setWritable和setCookieValidationKey都要做相应复制和修改
- 4.4 复制D:\upupw\vhosts\mxq.com\environments\dev和D:\upupw\vhosts\mxq.com\environments\prod文件夹下的frontend到相同目录下,改名为api
- 4.5 执行3.3的操作,通过 http://api.mxq.com 访问新添加的应用
5.添加数据库表前缀
打开D:\upupw\vhosts\mxq.com\common\config\main-local.php,
在db中增加子项
- ‘tablePrefix‘ => ‘mxq_‘,
复制代码
6.配置 Gii 为其添加允许外网访问的 IP 地址
打开D:\upupw\vhosts\mxq.com\backend\config\main-local.php,将
$config[‘modules‘][‘gii‘] = ‘yii\gii\Module‘;
复制代码
一行注释掉,改为
$config[‘modules‘][‘gii‘] = [
‘class‘ => ‘yii\gii\Module‘,
‘allowedIPs‘ => [‘127.0.0.1‘, ‘192.168.0.*‘]
];
192.168.0.*为您的IP地址
开发阶段进行init初始化操作会将其覆盖掉,所以我们直接修改D:\upupw\vhosts\mxq.com\environments\dev\backend\config\main-local.php,其他的应用也这样修改
7.开启国际化支持,源代码中文和设置时区、字符集默认UTF-8
因为所有应用都用中文,所以我们将开启设置在common中。
打开D:\upupw\vhosts\mxq.com\common\config\main.php,
在components一行上加入
‘language‘ => ‘zh-CN‘, // 启用国际化支持
‘sourceLanguage‘ => ‘zh-CN‘, // 源代码采用中文
‘timeZone‘ => ‘Asia/Shanghai‘, // 设置时区
8.禁用api应用的默认布局
打开D:\upupw\vhosts\mxq.com\api\config\main.php,
在components一行上加入
‘layout‘ => false,
9.去掉地址栏URL编码
http://yii2/index.php?r=site%2Flogin
http://yii2/index.php?r=site/login
以上两个网址,那个看起来更清晰明了呢?显而易见是第二个,为了达到这个效果,可以进行如下修改
打开D:\upupw\vhosts\mxq.com\vendor\yiisoft\yii2\web\UrlManager.php将
$url = "$baseUrl?{$this->routeParam}=" . urlencode($route);
改为
$url = "$baseUrl?{$this->routeParam}=" . $route;
10.为Controller制定布局模版
在init方法中写入
$this->layout = ‘‘;
引号之间填入您的布局文件名
11.Apache下三个应用公用一个高级模版配置
<VirtualHost *:80> DocumentRoot "D:/upupw/vhosts/mxq.com/backend/web" ServerName admin.mxq.com:80 ServerAlias admin ServerAdmin [email protected].mxq.com DirectoryIndex index.html index.htm index.php default.php app.php u.php ErrorLog logs/admin.mxq.com-error.log CustomLog logs/admin.mxq.com-access_%Y%m%d.log comonvhost php_admin_value open_basedir "D:\upupw\vhosts\mxq.com\;D:\upupw\memcached\;D:\upupw\phpmyadmin\;D:\upupw\temp\;C:\Windows\Temp\" <Directory "D:/upupw/vhosts/mxq.com/backend/web"> Options FollowSymLinks AllowOverride All Require all granted </Directory> <LocationMatch "/(inc)/(.*)$"> AllowOverride none Require all denied </LocationMatch> <LocationMatch "/(attachment|attachments|uploadfiles|avatar)/(.*).(php|php5|phps|asp|asp.net|jsp)$"> AllowOverride none Require all denied </LocationMatch> </VirtualHost> <VirtualHost *:80> DocumentRoot "D:/upupw/vhosts/mxq.com/frontend/web" ServerName www.mxq.com:80 ServerAlias www ServerAdmin [email protected] DirectoryIndex index.html index.htm index.php default.php app.php u.php ErrorLog logs/www.mxq.com-error.log CustomLog logs/www.mxq.com-access_%Y%m%d.log comonvhost php_admin_value open_basedir "D:\upupw\vhosts\mxq.com\;D:\upupw\memcached\;D:\upupw\phpmyadmin\;D:\upupw\temp\;C:\Windows\Temp\" <Directory "D:/upupw/vhosts/mxq.com/frontend/web"> Options FollowSymLinks AllowOverride All Require all granted </Directory> <LocationMatch "/(inc)/(.*)$"> AllowOverride none Require all denied </LocationMatch> <LocationMatch "/(attachment|attachments|uploadfiles|avatar)/(.*).(php|php5|phps|asp|asp.net|jsp)$"> AllowOverride none Require all denied </LocationMatch> </VirtualHost> <VirtualHost *:80> DocumentRoot "D:/upupw/vhosts/mxq.com/api/web" ServerName api.mxq.com:80 ServerAlias api ServerAdmin [email protected] DirectoryIndex index.html index.htm index.php default.php app.php u.php ErrorLog logs/api.mxq.com-error.log CustomLog logs/api.mxq.com-access_%Y%m%d.log comonvhost php_admin_value open_basedir "D:\upupw\vhosts\mxq.com\;D:\upupw\memcached\;D:\upupw\phpmyadmin\;D:\upupw\temp\;C:\Windows\Temp\" <Directory "D:/upupw/vhosts/mxq.com/api/web"> Options FollowSymLinks AllowOverride All Require all granted </Directory> <LocationMatch "/(inc)/(.*)$"> AllowOverride none Require all denied </LocationMatch> <LocationMatch "/(attachment|attachments|uploadfiles|avatar)/(.*).(php|php5|phps|asp|asp.net|jsp)$"> AllowOverride none Require all denied </LocationMatch> </VirtualHost>