php - Conflicting view helper names in Zend Framework - php

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.

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);

Related

Extend blade engine in Laravel to use different views directory

I am working on a Laravel project and I am very new to it. For now, I want to use blade templates to render views but I want it to search for views in different directories like <custom_dir>\views instead of default resources/views.
The <custom_dir> will be dynamic (it can be a variable).
Any ideas? I was thinking of a custom service provider and then extend the default function which renders views in Laravel inside it. But not sure how to implement it.
Edit:
I have user this link to extend the default functionality of include function in blade template engine. But this overrides the include functionality. I want to change the path and then call the default blade functionality
You could probably append the path to the configuration:
1) Statically, by modifying file config/view.php
'paths' => [
realpath(base_path('resources/views')),
//more paths here
],
2) Dynamically at runtime:
$paths = config('view.paths');
$paths[] = $newPathToAdd;
config(["view.paths" => $paths ]);
I suggest you use this in moderation otherwise you will just end up with a mess of directories with no real specified purpose.
You can create custom directories in resources\views directory and use them with something like this:
return view($customDirectory.'.index');
Where index is a template inside custom directory.

Symfony twig - Third party

i am currently working on a symfony project,
what i have:
app/Resources/views/mytemplate/
the folder mytemplate contains all of the important twig-views for my web app.
My question is, is there any possibility that third party members can create their own templates which override my "mytemplate" without creating controllers pointing to them ?
Like:
i have this template:
app/Resources/views/mytemplate/home/index.html.twig
An other person could create a new template in the same views directory like:
app/Resources/views/thirdparty/home/index.html.twig
to override my template.
is there any possibilty like this?
Greetings!
Well, to me, you have two possibilities :
The template that you want to be able to be redefined is the one specified with the method renderView() or similar in your controller : in this case, the possibilities are limitless. It's up to you to define the logic layer determining which template has to be rendered. You could for example force the user redefining the template to name it with a specific additional pattern, and then parse the right template to use thanks to a method inherited in all your controllers.
$content = $this->renderView(
$this->getInheritedTemplate('AcmeHelloBundle:Hello:index.html.twig'),
array('name' => $name)
);
The template that has to be redefined is one inherited in another twig template : In this case, it's almost the same. You could imagine writing your own Twig filter/function in order to retrieve the right template. The code should be very similar to the first case.
Hope this helped.

Phalcon PHP passing parameters to views that belongs to another controller

This is a real newbie question, but I have not used PHP and Phalcon very long and I am
sort of learning by studying examples, reading on internet and a bit of trial and error.
One thing that I got stuck on is how to pass variables to views that belongs to another controller.
If I want to pass a variable to a view in the same controller, lets call it showRoomController, then I simply use.
$this->view->setVar("id", $cars->id);
However, if I want to open the cars view from catalogueController, but from a page that belongs to showRoomController I use this:
return $this->forward("catalogue/cars");
How can I pass the cars id variable in the second example? Or do I need to use global variables?
I apologize if this is a very basic question that I probably should know.
Dispatcher's forward() method accepts params as well:
$this->dispatcher->forward(array(
"controller" => "myController",
"action" => "myAction",
"params" => array('name' => 'hello', 'surname' => 'world')
));
By default your view is a shared service in the DI. You can simply set parameters as you do in one controller, and when it did forward to another all of those parameters would still be there.
When you do $this->view in your controller it uses a magic method to get the view service from the DI, so if you do that in both controllers you will be referencing the same view.

Specifying domain in zend view url-helper

My application has two modules Product module and Blog module. Both modules use the same DB. There are two domains: the first pointing to the Product Module (www.mainsite.com) and the second to the blog module (www.blog.mainsite.com). There will be multiple links connecting from the Product Module and Blog module.
The issue is that i am using the zend url view helper. But i was not able to specify the domain to that view helper. when i call the view helper it is always returning my current domain. I have checked the zend framework manual, but nothing was found.
Is there any other option in zend to implement the same?
In case of fully qualified URI you should go for:
$this->_redirect('http://www.blog.mainsite.com/some/thing');
When you are calling the url helper in a view it actually calls route assembler. So I assume you should define two routes for each domain and use the view helper something like that:
echo $this->url (array ('action' => 'some', 'controller' => 'thing'), 'routeName');
You can try this- http://framework.zend.com/manual/1.12/en/zend.controller.router.html#zend.controller.router.routes.hostname (not tested)
You can also just concat $_SERVER['HTTP_HOST'] with the URL generated by URL view helper.
Use the ServerUrl view helper, along with the "//" notation for inheriting the current URL scheme:
echo '//' . $this->serverUrl() . $this->url(/* ... */);

zf2 resolve module view path

Starting from the skeleton application using beta3 how would you resolve the view path for a new module called Foo?
I have added below to the di config and now both modules action's render Foo's views.
'Zend\View\Resolver\TemplatePathStack' => array(
'parameters' => array(
'paths' => array(
'foo' => __DIR__ . '/../view',
),
),
),
I would expect Application\Controller\IndexController::indexAction() to render the views in Application and for Foo\Controller\IndexController::indexAction() to render Foo's views.
Note that questions like this help shape the direction of the stable framework. :)
One idea I've been toying with is to use the module as part of the view script resolution. Right now, the default used is "/"; my proposal is to use "//", as this would help prevent naming conflicts between modules; it also makes it much simpler to understand exactly what view script you are overriding if you use template maps.
You can use this approach today, but it will require manually setting the template on the view models you return from your controllers.
This doesn't currently work in ZF2 as there is no concept of taking the namespace into account when resolving view scripts. Discussions are currently ongoing on how best to tackle this.
For the time being, you have to name each controller differently. In general, we are recommending that you name the "primary" controller within a module after the module name. That is, the primary controller in the Foo module would be FooController.
You actually can do this; and it is not too bad....
Rob Allen himself had a blog post that basically makes this work... Notice you have to basically handle it as a module based loader that separates much of the work out so that we don't have controllers utilizing it: http://pastie.org/3824571

Categories