Zend framework 2 - 如何在视图助手中调用函数
Zend framework 2 - How to call a function in view helpers
我有一个查看助手class
namespace Users\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Userfunctions extends AbstractHelper
{
protected $count = 0;
public function test()
{
return 'testing';
}
public function __invoke($str, $find)
{
if (! is_string($str)){
return 'must be string';
}
if (strpos($str, $find) === false){
return 'not found';
}
return 'found';
}
}
在 module.config.php 中,我调用了视图助手 class,其中在 view_helpers 数组中调用了 Userfunctions。
'view_helpers' => array(
'invokables' => array(
'Userfunctions' => 'Users\View\Helper\Userfunctions',
// more helpers here ...
)
),
我想要实现的是,我需要调用测试函数()..
我怎样才能做到这一点..
我打电话知道
echo $this->Userfunctions("me","e");
这将调用调用函数并 return 找到..但我需要调用测试函数。
谢谢,
尝试以下操作:
echo $this->plugin('Userfunctions')->test()
在视图中按名称 $this->userfunctions('me', 'e');
调用您的插件实际上只是方法 Zend\View\Renderer\PhpRenderer::plugin('userfunctions')
的简写,所以使用 Bram Gerritsen 的答案是绝对正确的。
我要补充的是,许多默认视图助手遵循检查传递给 __invoke
函数的参数数量的约定,如果提供了 none,则插件返回。
例如
namespace Users\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Userfunctions extends AbstractHelper
{
protected $count = 0;
public function __invoke($str, $find)
{
if (0 === func_num_args()) {
return $this;
}
return $this->render($str, $find);
}
public function render($str, $find)
{
if (! is_string($str)){
return 'must be string';
}
if (strpos($str, $find) === false){
return 'not found';
}
return 'found';
}
public function test()
{
return 'testing';
}
}
然后您可以访问视图中的所有方法,以及 __invoke
的默认功能。
$this->userfunctions();
$this->userfunctions()->test();
$this->userfunctions()->render('me', 'e');
我有一个查看助手class
namespace Users\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Userfunctions extends AbstractHelper
{
protected $count = 0;
public function test()
{
return 'testing';
}
public function __invoke($str, $find)
{
if (! is_string($str)){
return 'must be string';
}
if (strpos($str, $find) === false){
return 'not found';
}
return 'found';
}
}
在 module.config.php 中,我调用了视图助手 class,其中在 view_helpers 数组中调用了 Userfunctions。
'view_helpers' => array(
'invokables' => array(
'Userfunctions' => 'Users\View\Helper\Userfunctions',
// more helpers here ...
)
),
我想要实现的是,我需要调用测试函数()..
我怎样才能做到这一点..
我打电话知道
echo $this->Userfunctions("me","e");
这将调用调用函数并 return 找到..但我需要调用测试函数。
谢谢,
尝试以下操作:
echo $this->plugin('Userfunctions')->test()
在视图中按名称 $this->userfunctions('me', 'e');
调用您的插件实际上只是方法 Zend\View\Renderer\PhpRenderer::plugin('userfunctions')
的简写,所以使用 Bram Gerritsen 的答案是绝对正确的。
我要补充的是,许多默认视图助手遵循检查传递给 __invoke
函数的参数数量的约定,如果提供了 none,则插件返回。
例如
namespace Users\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Userfunctions extends AbstractHelper
{
protected $count = 0;
public function __invoke($str, $find)
{
if (0 === func_num_args()) {
return $this;
}
return $this->render($str, $find);
}
public function render($str, $find)
{
if (! is_string($str)){
return 'must be string';
}
if (strpos($str, $find) === false){
return 'not found';
}
return 'found';
}
public function test()
{
return 'testing';
}
}
然后您可以访问视图中的所有方法,以及 __invoke
的默认功能。
$this->userfunctions();
$this->userfunctions()->test();
$this->userfunctions()->render('me', 'e');