简体   繁体   English

php-Zend Framework中的视图帮助程序名称冲突

[英]php - Conflicting view helper names in Zend Framework

I am using a module called Facebook which has a view helper called shareUrl . 我正在使用一个名为Facebookmodule ,该module具有一个名为shareUrl的视图助手。 This view helper gets the Facebook share URL for any URL. 该视图助手获取任何URL的Facebook共享URL。

However, I have recently added another module called Twitter which also has a view helper called shareUrl . 但是,我最近添加了另一个名为Twitter module ,该module还具有一个名为shareUrl的视图助手。

In Zend Framework version 2 or 3, within views, how can I call one shareUrl view helper versus the other? 在Zend Framework版本2或3中,在视图内,我该如何调用一个shareUrl视图助手而不是另一个?

Just to clarify, the code in my view looks like the following: 为了澄清起见,在我看来,代码如下所示:

$facebookShareUrl = $this->shareUrl('https://www.example.com/');
$twitterShareUrl  = $this->shareUrl('https://www.example.com/');

I would like $facebookShareUrl and $twitterShareUrl to store the return values of two different view helpers. 我希望$facebookShareUrl$twitterShareUrl存储两个不同的视图助手的返回值。

If you've got two helpers with the same name, only one is available as it is registered under the given name within the servicemanager (viewhelpermanager). 如果您有两个具有相同名称的助手,则只有一个可用,因为它以给定名称在servicemanager(viewhelpermanager)中注册。 If you switch them around with loading the modules in your application.config.php you can change the default. 如果通过在application.config.php加载模块来切换它们,则可以更改默认值。 But that is not a real solution to your problem. 但这不是您问题的真正解决方案。

So there are multiple ways to get the right viewhelper you need. 因此,有多种方法来获取所需的正确的viewhelper。

1) The best way is to setup an alias for the registered viewhelpers using their FQCN. 1)最好的方法是使用他们的FQCN为注册的viewhelper设置别名。 See some example code where we create aliases that can be used in the viewherlpers like $this->facebookShareUrl('exmaple.com') 请参阅一些示例代码,在这些代码中我们创建可以在查看器中使用的别名,例如$this->facebookShareUrl('exmaple.com')

return [
    'view_helpers' => [
       'aliases' => [
           'facebookShareUrl' => FacebookModule\Helper\ShareUrlHelper::class,
           'twitterShareUrl'  => TwitterModule\Helper\ShareUrlHelper::class,
       ],
    ]
]

2) Get the helper by its FQCN using the viewhelpermanager in the view itself, using the PhpRenderer instance. 2)使用PhpRenderer实例,在视图本身中使用PhpRenderer通过其FQCN获取帮助PhpRenderer Within a view.phtml file view.phtml文件中

$viewHelperManager = $this->getHelperPluginManager();
$facebookShareUrlHelper = $viewHelperManager->get(FacebookModule\Helper\ShareUrl::class);
$twitterShareUrlHelper = $viewHelperManager->get(TwitterModule\Helper\ShareUrl::class);

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

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