Symfony 3 - 渲染视图后替换字符串(注释)
Symfony 3 - replace string after render view (annotation)
渲染后我需要替换视图中的字符串。
我所有的控制器都使用注释 @Template("path").
我的控制器:
...
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 的事件系统 onKernelResponse
这将允许您在控制器操作后获取响应 return 并在发送之前修改响应。
要订阅活动,请关注 Syfmony's doc example。
您没有告诉我们您使用的是哪个版本的 Symfony,这些链接是 3.4.
希望对您有所帮助。
渲染后我需要替换视图中的字符串。 我所有的控制器都使用注释 @Template("path").
我的控制器:
...
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 的事件系统 onKernelResponse
这将允许您在控制器操作后获取响应 return 并在发送之前修改响应。
要订阅活动,请关注 Syfmony's doc example。 您没有告诉我们您使用的是哪个版本的 Symfony,这些链接是 3.4.
希望对您有所帮助。