我需要在生产服务器的嵌套目录中部署一个Symfony 2项目。实际上,这意味着所有 URL 都以/subdirectory/路径为前缀,即
http://host.com/subdirectory/project/web/app.php/survey
我不需要 URL 重写,也不打算设置它。仅通过上述 URL 访问该应用程序时应运行。
我遇到的问题是path
和asset
Twig 函数生成的所有链接都相对于服务器根目录 (/),而不是项目所在的子目录 (/subdirectory/)。Symfony 2中是否有任何配置参数可以全局覆盖相对路径?
我试图通过添加 HTML 标签来解决这个问题,但它不适用于链接。
更新:我欠你更多细节。我正在运行带有 mod_rewrite 版本的 IIS,因此您的部分建议可能仍然有效。
更新:理想情况下,我希望看到一个在 AppKernel 对象上设置根目录的解决方案 - 方法AppKernel::setRootDir()
如果它存在以补充现有的AppKernel::getRootDir()
奇怪...使用默认配置,路径/资产应处理子目录。您是否尝试过开箱即用的symfony发行版?在本地主机上,我主要以这种方式使用它。你介意发布你的配置文件吗?
无论如何。。。对于资产路径,您可以尝试配置:
#config.yml
templating:
assets_base_urls: { http: "http://yoursite.com/dir1", ssl: "https://yoursite.com/dir1" }
(http://symfony.com/doc/current/reference/configuration/framework.html#assets-base-urls)
对于路由器,您应该阅读:http://symfony.com/doc/current/cookbook/console/sending_emails.html#configuring-the-request-context-globally它可以为您提供有关更改路由器上下文的一些想法,例如:
# app/config/parameters.yml
parameters:
router.request_context.host: example.org
router.request_context.scheme: http
router.request_context.base_url: my/path
无论如何 - 上下文是根据请求信息自动设置的。尝试调试:
$context = $this->getContainer()->get('router')->getContext();
// you should be mainly interested in:
$context->getBaseUrl();
可以轻松创建侦听器类,如果有任何错误,它将手动设置您的基本 url:
class RouterContextListener {
protected $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onKernelRequest(Event $event)
{
$this->router->setBaseUrl('somedir');
}
}
您正在寻找重写基本规则,将其添加到您的.htaccess
中:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdirectory/project/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
也看看symfony-standard 2.3 .htaccess文件,mb它可以提供帮助
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::'2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app'.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
查看 app/config/routing.yml,其中您可以从捆绑包导入路由。该文件中的记录如下所示:
somename:
resource: "@YourBundle/Resources/config/routing.yml"
prefix: /
您可以在前缀中添加子目录,例如prefix: /subdirectory
然后所有生成的 URL 都将包含 subdirectory
.
要启用重写模块,请运行"apache2 启用模块重写":
sudo a2enmod rewrite
您需要重新启动 Web 服务器才能应用更改:
sudo service apache2 restart
虚拟主机应该是这样的 服务器名称 localhost.myproject.dev 服务器管理员webmaster@localhost
DocumentRoot /var/www/project_path
<Directory /var/www/project_path>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>
</Directory>
此选项是重定向您的索引.php、索引.html或其他任何内容:
Options Indexes FollowSymLinks MultiViews
它会重写你的 htacces!
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>
我在本地环境中的解决方案是在app/config/config.yml
中添加以下代码
framework:
assets:
base_urls:
- 'http://localhost/symfony_dir/'
无需重写!