嵌套目录中的 Symfony 2 项目 [英] Symfony 2 project in nested directory
问题描述
我需要在生产服务器的嵌套目录中部署一个 Symfony 2 项目.实际上,这意味着所有 URL 都以/subdirectory/路径为前缀,即
I need to deploy a Symfony 2 project in nested directory on production server. Effectively, this would mean that all URLs are prefixed with /subdirectory/ path, i.e.
http://host.com/subdirectory/project/web/app.php/survey
我不需要 URL 重写,也不打算设置它.仅通过上述 URL 访问时,该应用应该可以正常工作.
I don't need URL rewriting and are am not going to set it up. The app should work when accessed via above URL only.
我遇到的问题是 path
和 asset
Twig 函数生成的所有链接都相对于服务器根目录 (/),而不是项目所在的子目录 (/subdirectory/).Symfony 2 中是否有任何配置参数可以全局覆盖相对路径?
The problem I have is that all links generated by path
and asset
Twig functions are relative to server root (/), and not subdirectory the project is in (/subdirectory/). Is there any config parameter in Symfony 2 to override relative paths globally?
我尝试通过添加 HTML 标记来解决该问题,但它不适用于链接.
I tried to work around that problem by adding HTML tag, but it doesn't work for links.
更新:我欠你更多细节.我正在运行带有 mod_rewrite 版本的 IIS,因此您的部分建议可能仍然有效.
Update: I owe you further details. I'm running IIS with its version of mod_rewrite, so part of your suggestions may still be valid.
更新: 理想情况下,我希望看到一个在 AppKernel 对象上设置根目录的解决方案 - 方法 AppKernel::setRootDir()
是否存在以补充现有的 AppKernel::getRootDir()
.
Update: Ideally I would like to see a solution that sets root dir on AppKernel object - method AppKernel::setRootDir()
had it existed to complement existing AppKernel::getRootDir()
.
推荐答案
奇怪... 使用默认配置,路径/资产应该处理子目录.您是否尝试过开箱即用的 symfony 发行版?在本地主机上,我主要以这种方式使用它.你介意发布你的配置文件吗?
Strange... with default config, path/asset should handle subdirectories. Have you tried it with symfony distribution out-of-the-box? On localhost im mostly using it this way. Would you mind posting your config file?
无论如何...对于资产路径,您可以尝试配置:
Anyway... For assets paths you could try to configure:
#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它可以为您提供有关更改路由器上下文的一些想法,例如:
For router you should read: http://symfony.com/doc/current/cookbook/console/sending_emails.html#configuring-the-request-context-globally It could give you some ideas about changing router context like:
# app/config/parameters.yml
parameters:
router.request_context.host: example.org
router.request_context.scheme: http
router.request_context.base_url: my/path
无论如何 - 上下文是根据请求信息自动设置的.尝试调试:
Anyway - context is automaticlly set based on request information. Try to debug:
$context = $this->getContainer()->get('router')->getContext();
// you should be mainly interested in:
$context->getBaseUrl();
可以轻松创建侦听器类,如果无论如何它是错误的,它将手动设置您的基本网址:
It is possible to easily create listener class which will manually set your base url if anyhow it is wrong:
class RouterContextListener {
protected $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onKernelRequest(Event $event)
{
$this->router->setBaseUrl('somedir');
}
}
这篇关于嵌套目录中的 Symfony 2 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!