shopwind 2021-11-17 17:40:40 3756次浏览 0条评论 3 2 0

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

首先我们先看下Yii2高级应用程序模板的目录结构,如下图所示:
yii2目录结构.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

觉得很赞
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册