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

138 阅读1分钟

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

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

案例源码下载:gitee.com/shopwind/yi…