Yii2管理员和前端分开

时间:2017-07-17 20:05:20

标签: yii2

我发了很多帖子,但仍然无法实现我应该如何分开它们。我说的是:www.mysite.com(前端)和www.mysite.com/admin(backend)。也尝试了文档说的方式

<VirtualHost *:80>
        ServerName frontend.dev
        DocumentRoot "/path/to/yii-application/frontend/web/"

        <Directory "/path/to/yii-application/frontend/web/">
            # use mod_rewrite for pretty URL support
            RewriteEngine on
            # If a directory or a file exists, use the request directly
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            # Otherwise forward the request to index.php
            RewriteRule . index.php

            # use index.php as index file
            DirectoryIndex index.php

            # ...other settings...
            # Apache 2.4
            Require all granted

            ## Apache 2.2
            # Order allow,deny
            # Allow from all
        </Directory>
    </VirtualHost>

    <VirtualHost *:80>
        ServerName backend.dev
        DocumentRoot "/path/to/yii-application/backend/web/"

        <Directory "/path/to/yii-application/backend/web/">
            # use mod_rewrite for pretty URL support
            RewriteEngine on
            # If a directory or a file exists, use the request directly
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            # Otherwise forward the request to index.php
            RewriteRule . index.php

            # use index.php as index file
            DirectoryIndex index.php

            # ...other settings...
            # Apache 2.4
            Require all granted

            ## Apache 2.2
            # Order allow,deny
            # Allow from all
        </Directory>
    </VirtualHost>

但从我所有的红色来看,我认为这不是正确的方式。帮助你寻求帮助我几个小时就已经没有结果了。提前谢谢!

1 个答案:

答案 0 :(得分:1)

无需编写.htaccess,您可以通过将index.php的位置从“frontend / web / index.php”更改为“/frontend/index.php”以及index.php的代码来实现此目的。< / p>

<?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__ . '/config/bootstrap.php');

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

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

您可以在admin文件夹中执行相同的更改。请记住,您需要将后端文件夹重命名为“admin”。 还要创建一个与index.php文件并行的“Assets”文件夹。在我们的例子中,“frontend”中已经存在“assets”文件夹。因此无需创建。