[英]How to bind phtml view file with custom view helper
I would like to create custom view helper for a few controls on my page. 我想为页面上的一些控件创建自定义视图助手。 I decided to use custom view helper because these controls may be different for different users (logged in and not logged in).
我决定使用自定义视图帮助程序,因为这些控件对于不同的用户(登录和未登录)可能有所不同。 However I would like to set layout file for my view instead of just returning html code in __invoke function.
但是我想为我的视图设置布局文件,而不仅仅是在__invoke函数中返回html代码。 Is there a possibility to do that?
有可能这样做吗? Or only subviews allow to set phtml view file for them?
还是只有子视图允许为其设置phtml视图文件?
Edited (because a lot of confusions which my question made) This is my view helper invoke function: 编辑(因为我的问题引起了很多混乱)这是我的视图助手调用函数:
public function __invoke()
{
$authService = new AuthenticationService();
$body = "<ul class='nav navbar-nav navbar-right'>";
if(!$authService->hasIdentity())
{
$body .= $this->wrapLi($this->makeUrl($this->view->url('users/default', array('controller' => 'register')), $this->view->translate('Register')));
$body .= $this->wrapLi($this->makeUrl($this->view->url('users/default', array('controller' => 'login')), $this->view->translate('Login')));
}
if($authService->hasIdentity())
{
$body .= $this->wrapLi($this->makeUrl($this->view->url('users/default', array('controller' => 'logout')), $this->view->translate('Logout')));
}
$body .= "</ul>";
return $body;
}
As you may see it returns just html. 如您所见,它仅返回html。 I use this helper in layout.html this way:
我通过以下方式在layout.html中使用此帮助程序:
<?php echo $this->LoginViewHelper(); ?>
I would like to know if there is another way to create html which is returned by invoke function. 我想知道是否存在另一种创建HTML的方法,该方法由invoke函数返回。 If there is a possibility to make template.
如果有可能制作模板。
I think you are a bit lost, the way to load html is with views, that are usually called from a controller, you can recognize them because they have the word action at the end of the method name. 我认为您有点迷失了,加载html的方法是使用视图,通常是从控制器调用的,您可以识别它们,因为它们在方法名称的末尾有“动作”一词。
I would recommend you to install the zend framework 2 skeleton, which provide already a module with some views and controller. 我建议您安装zend framework 2框架,该框架已经为模块提供了一些视图和控制器。
But if you want to do something like what you are trying to do now. 但是,如果您想做类似现在想要做的事情。 You could create a new folder called Helper on your Application module, define a namespace like as first line:
您可以在应用程序模块上创建一个名为Helper的新文件夹,并定义一个名称空间,如第一行所示:
namespace Application\Helper;
then create a static method: 然后创建一个静态方法:
class H {
public static function LoginViewHelper(){
}
}
on your view call it like this 在您看来,这样称呼它
<?php echo \Application\Helper\H::LoginViewHelper() ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.