Reputation: 2730
I am using a module
called Facebook
which has a view helper called shareUrl
. This view helper gets the Facebook share URL for any URL.
However, I have recently added another module
called Twitter
which also has a view helper called shareUrl
.
In Zend Framework version 2 or 3, within views, how can I call one shareUrl
view helper versus the other?
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.
Upvotes: 1
Views: 90
Reputation: 1382
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). If you switch them around with loading the modules in your application.config.php
you can change the default. But that is not a real solution to your problem.
So there are multiple ways to get the right viewhelper you need.
1) The best way is to setup an alias for the registered viewhelpers using their FQCN. See some example code where we create aliases that can be used in the viewherlpers like $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. Within a view.phtml
file
$viewHelperManager = $this->getHelperPluginManager();
$facebookShareUrlHelper = $viewHelperManager->get(FacebookModule\Helper\ShareUrl::class);
$twitterShareUrlHelper = $viewHelperManager->get(TwitterModule\Helper\ShareUrl::class);
Upvotes: 6