Symfony 3-在渲染视图(注释)后替换字符串
渲染后,我需要替换视图中的字符串。 我的所有控制器都使用注释@模板(“路径”) 我的控制器:Symfony 3-在渲染视图(注释)后替换字符串,symfony,templates,replace,annotations,render,Symfony,Templates,Replace,Annotations,Render,渲染后,我需要替换视图中的字符串。 我的所有控制器都使用注释@模板(“路径”) 我的控制器: ... class AboutController extends Controller { /** * @Route("/about-us", name="about") * @Method("GET") * @Template("@AppBundle/Resources/views/About/index.html.twig") */ publ
...
class AboutController extends Controller
{
/**
* @Route("/about-us", name="about")
* @Method("GET")
* @Template("@AppBundle/Resources/views/About/index.html.twig")
*/
public function indexAction()
{
}
}
...
我知道不用注释就可以做到:
...
class AboutController extends Controller
{
/**
* @Route("/about-us", name="about")
* @Method("GET")
*/
public function indexAction()
{
$content = $this->renderView('AppBundle/Resources/views/About/index.html.twig', []);
$content = str_replace('my text', 'my new text', $content);
return new Response($content);
}
}
...
如何使用注释(@template)?我认为您应该使用Symfony的事件系统 这将允许您在控制器操作后获取响应并返回它,并在发送之前修改响应 要订阅活动,请执行以下操作。 您没有告诉我们您使用的是哪个版本的Symfony,这些链接是3.4
希望这有帮助。在你的小树枝文件中使用变量是否更有意义?webiste很大,当链接是另一个域时,我必须替换所有指向js哈希的链接:)好的解决方案!谢谢