java动态路由_Symfony,动态路由

我认为通过在内核请求上添加一个监听器,可以根据用户动态加载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在Listener构造函数中定义为\ Twig_Loader_Filesystem

希望它可以给你一个线索

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值