简体   繁体   English

在ZF2中如何使视图函数在控制器中运行

[英]In ZF2 how to make view functions to run in controller

I wanted the functionalities of view files to run in controller file also. 我希望视图文件的功能也在控制器文件中运行。

For example, I wanted $this->escapeHtml() which runs in view file alone to run in controller through some means like $this->...->escapeHtml() 例如,我想要$this->escapeHtml()在视图文件中单独运行,通过某些方式在控制器中运行,如$this->...->escapeHtml()

Is this possible? 这可能吗? Kindly help. 请帮助。

You need to get the ViewHelperManager and extract the EscapeHtml helper. 您需要获取ViewHelperManager并提取EscapeHtml帮助程序。 This is a one example how to do it from the controller: 这是一个如何从控制器执行此操作的示例:

$viewHelperManager = $this->getServiceLocator()->get('ViewHelperManager');
$escapeHtml = $viewHelperManager->get('escapeHtml'); // $escapeHtml can be called as function because of its __invoke method       
$escapedVal = $escapeHtml('string');

Note that it is recommended to escape and display the output in the view scripts and not in the controller. 请注意,建议在视图脚本中转义并显示输出,而不是在控制器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM