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

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

首先我们先看下Yii2高级应用程序模板的目录结构,如下图所示:


QQ截图20211117160933.png

第一步: 需要把网址的运行目录指向 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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容