在zf2 中 使用自定义视图助手

1、自定义视图类
//./module/Core/src/Core/Helper/SpecialPurpose.php
namespace Core\Helper;    
use Zend\View\Helper\AbstractHelper;    
class SpecialPurpose extends AbstractHelper
{
    protected $count = 0;    
    public function __invoke()
    {
        $this->count++;
        $output = sprintf("I have seen 'The Jerk' %d time(s).", $this->count);
        return htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
    }
}

2、注册自定义视图

// ./module/Core/config/module.config.php 

return array(
    'view_helpers' => array(
        'invokables' => array(
            // generic view helpers
            'specialPurpose' => 'Core\Helper\SpecialPurpose',
        ),
    ),
);

3、在view 中使用

<?php echo $this->specialPurpose(); ?>

你可能感兴趣的:(view,自定义,helper,zf2,视图助手)