Symfony,动态路由
Symfony, dynamic routing
我有一个包含多个 skins/templates 的 symfony 项目,它们有自己的路线,有人知道正确设置吗?
- 每个 skin/template 都是它自己的捆绑包,因为它不仅是皮肤和资产,而且可能还有某些皮肤中可能存在的服务。
- 主机名决定皮肤。
- 使用自定义 RouteLoader 加载目标包的 route.yml。
自定义 RouteLoader 可以完成这项工作——但是生成的路由正在被缓存,据我所知,没有办法阻止路由缓存。
一些建议是:
- 正在创建一个 /{dynamic} 路由,因此手动形成路由..但我不想丢弃路由器的那部分功能,或者重构整个项目..
- 在路由前加上模板标识符。这将需要我加载所有 route.yml 文件,这是不可能的,因为它们的共享路径。
有人吗?真的不能做多个项目,皮肤的数量在20-30左右~。
此设置的原因是因为它的目标是内容即服务 .. 服务,多个客户端将项目用作平台,并且它们的设置决定了使用哪些模板。
Symfony2 已经支持开箱即用的主机感知路由,如下所示:
website_customer_1:
path: /
host: customer1.example.com
defaults: { _controller: Customer1Bundle:Main:startPage, theme: template1 }
website_customer_2:
path: /
host: customer2.example.com
defaults: { _controller: Customer1Bundle:Main:startPage, theme: template2 }
听起来您想根据主机名动态加载包?由于缓存,Symfony 2 不会发生这种情况。尤其是服务。
最好的办法是为每个皮肤设置一个应用程序,然后执行一些 url majic 来执行所需的 app.php 文件。很明显,因为您已经为每个皮肤定义了一个包,所以数量有限,所以拥有多个应用程序应该不会太多或成为负担。
您或许可以解决模板问题。您仍然需要加载所有皮肤包,但您可以随心所欲地使用模板名称或路径,并且可能会得到一些有用的东西。
但是服务呢?除非您开始将主机名附加到服务 ID,否则我看不到任何解决方法。
我认为可以通过在内核请求上添加侦听器来根据您的用户动态加载 twig 模板。
我可以给你一段代码,希望对你有所帮助:
/**
* On Kernel Request triggers the request to get the user config
* then adds TWIG paths depending on user TemplateName
*/
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
//$userConfig = Retrieve your user config
if (null === $userConfig->getTemplateConfig()->getTemplate()->getName())
{
throw new TemplateConfigNotFoundException(sprintf("Could not find TemplateConfig for %s", $userConfig->getName()));
}
$template = $userConfig->getTemplateConfig()->getTemplate()->getName();
$path = sprintf('%s/../../%s/Resources/views', __DIR__, ucfirst($template));
if (!is_dir($path)) {
throw new TemplateNotFoundException(sprintf("Could not find template %s", $template));
}
$this->loader->prependPath($path);
$this->loader->addPath(sprintf('%s/../Resources/views/Default', __DIR__));
}
在监听器构造函数中将 $this->loader 定义为 \Twig_Loader_Filesystem
希望能给大家一个线索
我有一个包含多个 skins/templates 的 symfony 项目,它们有自己的路线,有人知道正确设置吗?
- 每个 skin/template 都是它自己的捆绑包,因为它不仅是皮肤和资产,而且可能还有某些皮肤中可能存在的服务。
- 主机名决定皮肤。
- 使用自定义 RouteLoader 加载目标包的 route.yml。
自定义 RouteLoader 可以完成这项工作——但是生成的路由正在被缓存,据我所知,没有办法阻止路由缓存。
一些建议是:
- 正在创建一个 /{dynamic} 路由,因此手动形成路由..但我不想丢弃路由器的那部分功能,或者重构整个项目..
- 在路由前加上模板标识符。这将需要我加载所有 route.yml 文件,这是不可能的,因为它们的共享路径。
有人吗?真的不能做多个项目,皮肤的数量在20-30左右~。
此设置的原因是因为它的目标是内容即服务 .. 服务,多个客户端将项目用作平台,并且它们的设置决定了使用哪些模板。
Symfony2 已经支持开箱即用的主机感知路由,如下所示:
website_customer_1:
path: /
host: customer1.example.com
defaults: { _controller: Customer1Bundle:Main:startPage, theme: template1 }
website_customer_2:
path: /
host: customer2.example.com
defaults: { _controller: Customer1Bundle:Main:startPage, theme: template2 }
听起来您想根据主机名动态加载包?由于缓存,Symfony 2 不会发生这种情况。尤其是服务。
最好的办法是为每个皮肤设置一个应用程序,然后执行一些 url majic 来执行所需的 app.php 文件。很明显,因为您已经为每个皮肤定义了一个包,所以数量有限,所以拥有多个应用程序应该不会太多或成为负担。
您或许可以解决模板问题。您仍然需要加载所有皮肤包,但您可以随心所欲地使用模板名称或路径,并且可能会得到一些有用的东西。
但是服务呢?除非您开始将主机名附加到服务 ID,否则我看不到任何解决方法。
我认为可以通过在内核请求上添加侦听器来根据您的用户动态加载 twig 模板。
我可以给你一段代码,希望对你有所帮助:
/**
* On Kernel Request triggers the request to get the user config
* then adds TWIG paths depending on user TemplateName
*/
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
//$userConfig = Retrieve your user config
if (null === $userConfig->getTemplateConfig()->getTemplate()->getName())
{
throw new TemplateConfigNotFoundException(sprintf("Could not find TemplateConfig for %s", $userConfig->getName()));
}
$template = $userConfig->getTemplateConfig()->getTemplate()->getName();
$path = sprintf('%s/../../%s/Resources/views', __DIR__, ucfirst($template));
if (!is_dir($path)) {
throw new TemplateNotFoundException(sprintf("Could not find template %s", $template));
}
$this->loader->prependPath($path);
$this->loader->addPath(sprintf('%s/../Resources/views/Default', __DIR__));
}
在监听器构造函数中将 $this->loader 定义为 \Twig_Loader_Filesystem
希望能给大家一个线索