Yii2高级应用程序模板修改前台后台的访问路径

用Yii2高级应用程序模板搭建的系统,根目录设置为wwwroot,访问路径是这样的,前台访问路径:http://localhost/frontend/web, 后台访问路径:http://localhost/backend/web, 这样的访问不是很方便。那么怎么样才能修改为 http://localhost 和 http://localhost/admin 这样的访问路径呢?

首先我们先看下Yii2高级应用程序模板的目录结构,如下图所示:
在这里插入图片描述
第一步: 需要把网址的运行目录指向 frontend/web 文件。这样就能 http://localhost 访问到前台。下面步骤就解决怎么访问后台。
第二步: 在frontend/web目录下面建一个文件夹admin。把原本在backend/web下面的文件迁移到admin下面。
第三步: 修改frontend/web/admin 目录下面的index.php文件,内容如下:

<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require __DIR__ . '/../../../vendor/autoload.php';
require __DIR__ . '/../../../vendor/yiisoft/yii2/Yii.php';
require __DIR__ . '/../../../common/config/bootstrap.php';
require __DIR__ . '/../../../backend/config/bootstrap.php';

$config = yii\helpers\ArrayHelper::merge(
    require __DIR__ . '/../../../common/config/main.php',
    require __DIR__ . '/../../../common/config/main-local.php',
    require __DIR__ . '/../../../backend/config/main.php',
    require __DIR__ . '/../../../backend/config/main-local.php'
);

(new yii\web\Application($config))->run();

第四步: 浏览器输入http://localhost/admin 看是否能正常访问到后台。Nginx服务器需要配置伪静态:

location / {
  #Redirect everything that isn't a real file to index.php
  try_files $uri $uri/ /index.php$is_args$args;
}
#If you want a single domain name at the front and back ends
location /admin {
  try_files $uri $uri/ /admin/index.php$is_args$args;
}

具体案例可以参考:
http://test.shopwind.net
http://test.shopwind.net/admin

案例源码下载:https://gitee.com/shopwind/yii2-shopwind

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值