Yii2高级模板后端和前端位于同一域


Yii2 advanced template backend and frontend on same domain

我真的很欣赏Yii2高级模板将后端和前端划分为单独的目录,保持其结构化,但我不知道如何将其部署到服务器上。一天下来,我必须将它上传到apache服务器,并且必须在http://domain.com/,而后端必须位于http://domain.com/admin/.服务器基于apache。

这将如何实现?

谢谢!

要访问前端的后端应用程序,可以使用符号链接:

在linux命令shell 中

ln -s project_dir/backend/web project_dir/frontend/web/admin

或在windows 上

mklink /J project_dir'frontend'web'admin project_dir'backend'web

只需将"project_dir"替换为项目的路径

将所有文件复制到站点的根文件夹中。在站点根目录中创建admin文件夹。将frontend/web的内容复制到站点根目录,并将backend/web的内容拷贝到站点根目录/admin文件夹。并将root/index.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__ . '/frontend/config/bootstrap.php');
$config = yii'helpers'ArrayHelper::merge(
    require(__DIR__ . '/common/config/main.php'),
    require(__DIR__ . '/common/config/main-local.php'),
    require(__DIR__ . '/frontend/config/main.php'),
    require(__DIR__ . '/frontend/config/main-local.php')
);
(new yii'web'Application($config))->run();

并将root/admin/index.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')
);
$application = new yii'web'Application($config);
$application->run();

在站点的根目录中,您需要放置一个.htaccess文件。如果URL包含"admin",您将重定向到后端的物理和真实路径。否则,使用物理和真实路径到达前端。此外,您还可以通过在前端和后端的目录中放置另一个.htaccess来从URL中删除"/web/"。所以根htaccess将重定向到前端或后端目录,然后这些目录将传递到web。这是一种菊花链方法,但它确实有效。

您也可以使用符号链接或设置虚拟主机(vhosts)。老实说,vhost方法将是最好的情况。然而,这也能完成任务。除非你有很多流量,否则它不会对性能产生太大影响。

站点的根目录。htaccess:

RewriteEngine on
RewriteRule ^admin/(.*)$ backend/$1 [L]
RewriteRule ^(.*)$ frontend/$1 [L]

我使用第一条规则来实现site.com/admin->映射到site.com/backend。然后backend有映射到web的htaccess。我刚刚为你添加了第二条规则,我还没有测试过。所以如果url路径是"admin",则传递到后端,否则所有其他内容都会传递到前端。

.htaccess在"前端"answers"后端"目录中(从url隐藏web):

Options -Indexes
RewriteEngine on
RewriteRule ^(.*)$ web/$1 [L]

.htaccess在"前端/web"answers"后端/web"目录中(根据漂亮的url):

RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

在backend/config/main.php和frontend/config-main.php中,添加以下内容:

'components' => [
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'enableStrictParsing' => false,
        'rules' => [
            // ...
        ],
    ],
],

这将启用漂亮的URL。


我会把你的整个项目放在"public_html"目录上,或者放在你的公共web根目录上。这样,所有文件都不可访问。然后将web目录的内容物理复制到需要的位置。即:将"frontend/web"的内容复制到您网站的根目录中。然后创建一个名为"admin"的文件夹,并将"backend/web"的内容放在其中。然后编辑index.php文件,将路径调整为yii。


您应该真正研究像Heroku、CloudControl和OpenShift这样的云主机。我个人喜欢OpenShift。然后,您可以使用"git push"上传您的更改,而不是使用老式的FTP。