解压后访问首页入口文件:./web/index.php
。
一开始打开会出错,因为没有写 Cookie,需要做以下配置:
// ./config/web.php
//...
'request' => [
'cookieValidationKey' -> 'a', // 随便设置一个值
//...
./init
(*nix)或 ./init.bat
(Windows),选择是开发环境(0
)还是生产环境(1
),接下来在“是否在开发环境下初始化应用”中填 yes
。./yii
(*nix)或 ./yii.bat
(Windows),确认版本。// ./common/config/main-local.php
//...
'db' => [
//...
'dsn' => 'mysql:host=localhost;dbname=yii2advanced',
'username' => 'root',
'password' => '',
'charset' => 'utf8'
'
根据上面的信息新建数据库(如 yii2advanced
)。
执行 ./yii migrate
(*nix)或 ./yii.bat migrate
(WIndows),输入 yes
,生成数据。
4. 访问前台首页入口文件:./frontend/web/index.php
;访问后台首页入口文件:./backend/web/index.php
。它们完全重复,两套系统共用数据库的账号密码,在前台注册账户可以登录前台和后台。
(以 Basic 为例)
./
├─Controllers/ 控制器
│ └─SiteController.php 控制器
├─Views/ 视图
│ ├─layout/
│ ├─site/ 站点
│ │ └─*.php 站点下的视图
// ./Controllers/SiteController.php
//...
class SiteController extends Controller{
//...
public function actionIndex() { //主页的加载
return $this -> render('index'); //在./Views/site/index.php中有对应视图
}
//...
//可以参照上面的方式写控制器方法
public function actionHello() { //方法以action开头,后面是URL相关的字符串
echo '...'; //当访问 ./web/index.php?r=site/hello时会调用这个方法
}
}
//...
// ./config/web.php
//...
if (YII_ENV_DEV) {
//...
$config['modules']['gii'] = ['class' => 'yii\gii\Module']; //如果是开发者模式,已经打开并配置好
//...
./web/index.php?r=gii