我刚刚按照文档将 Symfony 应用程序拆分为多个应用程序: https://symfony.com/doc/current/configuration/multiple_kernels.html
底部写着
Additionally, you might need to update your web server configuration to set the APP_ID=admin under a different domain.
。我该怎么做?
我在本地计算机上使用 Apache,我的服务器使用 NGINX。
我当前的本地配置:
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Users/alexandra/dev/Web/PROJECT/public
<Directory /Users/alexandra/dev/Web/PROJECT/public>
Require all granted
AllowOverride None
DirectoryIndex index.php
Options -Indexes
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 307 ^/$ /index.php/
</IfModule>
</IfModule>
</Directory>
</VirtualHost>
首先我必须说,很少需要拆分您的申请。如果您认为不同的应用程序需要相同的捆绑包/库和相同的
config.yaml
设置,那么我首先想到的并不是像这样拆分应用程序。
通常,如果您需要不同的用户用于前端和管理,您可以通过为不同的防火墙定义不同的 UserProvider 来将其拆分,该防火墙监听
/admin
子路径上的所有内容。
但是回答你的问题:
有几种方法,但我认为最简单的是使用
SetEnv
apache 指令,该指令记录在此处:https://httpd.apache.org/docs/2.4/mod/mod_env.html.
唯一的要求是您的网络服务器必须安装 mod_env apache 模块。然后您可以使用
SetEnv
指令设置 apache 配置。
<VirtualHost *:80>
Servername yourdomain.com
//...
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_env.c>
SetEnv APP_ID [the-yourdomain.com-identifier-here]
</IfModule
//...
</VirtualHost>
<VirtualHost *:80>
Servername anotherdomain.com
//...
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_env.c>
SetEnv APP_ID [the-anotherdomain.com-identifier-here]
</IfModule
//...
</VirtualHost>
还有其他方法,但它们需要您自定义
Kernel.php
文件。就像为每个域加载不同的 .env
文件,或者更改 APP_ID
变量来为域添加后缀一样。可能还有更多方法,但我认为 SetEnv
指令是最好的方法。