Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()

Is this possible? Kindly help.

like image 738
Beniston Avatar asked Nov 28 '22 08:11

Beniston


1 Answers

You need to get the ViewHelperManager and extract the EscapeHtml helper. 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.

like image 162
Stoyan Dimov Avatar answered Dec 10 '22 20:12

Stoyan Dimov