一、在config/web.php中添加如下代码(开启debug工具和gii工具)
if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = [ 'class' => 'yiidebugModule', 'allowedIPs' => ['192.168.*'] ]; $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = [ 'class'=>'yiigiiModule', 'allowedIPs'=>['192.168.*'], ]; }
注:debug工具要在入口文件中开启
defined('YII_DEBUG') or define('YII_DEBUG', true);
注:gii工具需要下载
php composer.phar
require
--prefer-dist yiisoft/yii2-gii
"*" 或者
composer.json 中加入 "yiisoft/yii2-gii"
:
"*"
二、配置url
(1)在config/web.php 的 $config 中加入如下代码
'components' => [ 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, ], ],
(2)apache 部署 yii 虚拟主机
(2.1)直接在Apache配置文件或者在一个虚拟主机的配置文件中进行一次性配置
<VirtualHost *:80>
ServerAdmin basic.com
DocumentRoot "/web_data/basic/web"
ServerName basic.com
ErrorLog "logs/basic.com-error_log"
CustomLog "logs/basic.com-access_log" common
<Directory /web_data/basic/web>
AllowOverride All
Options FollowSymLinks
RewriteEngine on
# 如果请求的是真实存在的文件或目录,直接访问
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# 如果请求的不是真实文件或目录,分发请求至 index.php
RewriteRule . index.php
</Directory>
</VirtualHost>
(2.2)虚拟主机配置 + 文件配置
(1)在虚拟机配置文件中添加如下配置
<VirtualHost *:80>
ServerAdmin 2022281825@qq.com
ServerName admin.yii.local
DocumentRoot /var/www/html/myYii/backend/web
ErrorLog ${APACHE_LOG_DIR}/admin.yii.local-error.log
CustomLog ${APACHE_LOG_DIR}/admin.yii.local-access.local combined
<Directory /var/www/html/myYii/backend/web>
AllowOverride All
</Directory>
</VirtualHost>
(2)在web目录下新建".htaccess",并在其中写入如下代码
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
三、配置数据库
编辑 config/db.php
return [ 'class' => 'yiidbConnection', 'dsn' => 'mysql:host=localhost;dbname=yii', 'username' => 'root', 'password' => '123456', 'charset' => 'utf8', ];