Zend Framework 2 - Set Template Path for Module - php

I googled alot and couldn't come up with an answer...
I'm using the tutorial-skeleton application. It automatically includes under 'view/album/album' the html files corresponding to my actions like add or index.
I'm using a submodule and the standard loading won't find my html-files. I followed this guide for setting a custom template path. This works for the index because here I use a ViewModel instance.
But my add/delete/edit actions just return an array like this one.
Is there a way to tell Zend that it should use a different directory to look for the views?
PS: I also tried this injectTemplate approach but no luck. It just sets the Controller namespace/path which is ok in my case.

This was an project specific issue...
I used MasterData as top namespace. When creating the directory tree in my module\MasterData\view I wrote masterdata instead of master-data. This caused the not finding of my views.
A dumb one... I know.

Related

Overriding the way Twig finds the views?

I'm new to Symfony, and so far I just love it!
Right now, I'm playing around with different personal hobby projects and I've come across something I do not seem to be able to wrap my head around.
I'm running Symfony 3.3.6 right now. I'm trying to kind of add a theme functionality to my project. Right now, I've got my Bundles that got their own views, which I can overwrite inside of app/Resources/views/. So right now, out of the box Symfony kind of supports a base view, with overwriting possibility.
What I would like to achieve is one more level of fallback.
I would like to make twig FIRST look into a theme folder, where the [THEME] would be a config.
1) Look for file inside of /app/theme/[THEME]/resources/view
2) Look for file inside of /app/theme/[THEME]/resources/[BUNDLE]/view
I would also like this to happen from inside a bundle, for example ThemeBundle.
Basically I would like to have a Bundle, that would take of the logic for Twig on how to find the views. It's really important that all the code, right now, would still work. So I do not wish to add a a path variable or something similar, but I would actually like to modify the behavior of TwigBundle from my own Bundle!
Is this sort of thing possible?
Twig has a built-in feature to achieve what you want called namespaces.
Twig will always load the first template that exists in a namespace, starting from the first configured path.
Configure the paths in your app/config/config.yml like this:
twig:
# ...
paths:
'%kernel.project_dir%/vendor/acme/themes/theme1': theme
'%kernel.project_dir%/vendor/acme/themes/theme2': theme
'%kernel.project_dir%/vendor/acme/themes/common': theme
Then use the namespace in your template like this:
{{ include('#theme/header.twig') }}
See the documentation chapter Multiple Paths per Namespace.
In order to dynamically add more paths living inside bundle directories you can use a compiler pass inside the corresponding bundle to add them.

Zend MVC - new page is not found

I'm pretty new to php and Zend in general, so I'll try to be as clear as I can. I have taken over a complete web project written in PHP.
In the main layout is script partials/menu.phtml, where is a list of few pages. And I wanted to add another one (called pricelist). I created a new pricelist.phtml file in directory info/ with other pages leading from this menu (in view).
Then in InfoController I created new public function pricelistAction() which includes
$this->view->headTitle()->append('Info');
$this->view->headTitle()->append('Pricelist');
Nothing is written in model. When I run localhost and click Info - Pricelist, the page is not found. I have no clue what I might have forgotten or what is wrong. I'm stuck on this for several days and starting to be desperate. I really appreciate any help. Thank you very much!
Two things:
Make sure there is a proper route in your module.config file that causes this action to be triggered. /baseroute/:action or /baseroute/pricelist...something like that
There are several ways of loading views/templates: Some developers require you to set new view files up explicitly in the view_manage=>template_map section of your module.config file, rather than let zend handle it automatically.
It's hard to guess without seeing your code/directory structure but this is where I would start.

CodeIgniter module loading creates path clash with local classes?

Using CodeIgniter, from a controller (application/controllers/home.php), observe the line:
$this->load->library(array('account/authentication'))
AFAIK, this will:
Load 'authentication.php' from
application/modules/account/libraries/authentication.php
Load 'authentication.php' from application/libraries/account/authentication.php
So, what if both exist? Experimenting, it seemed like CI looks for the first one, and if no such exists, it loads the second one. Isn't that kind of weird behavior?? The two files might have nothing to do with each other.
Isn't there a way of unambiguously declaring if you are referring to a local file or to a module file?
AFAIK, there is no default directory application/modules and I can't find any reference for it in CI's docs and source.
I would say that CI's behavior is according to no.2
And indeed taking a closer look at CI's loader (system/core/Loader.php) reveals the following:
We start at function 'library'
Which calls function '_ci_load_class'
'_ci_load_class' disassembles the library name you passed into two parts: subdir and class name.
First it looks for the class in libraries/subdir/class. If it finds it - good, it loads it.
If the class was not found there, it looks for it in some other predefined pathes ($this->_ci_library_paths).
If the library is still not found, it makes a last attempt and looks for it in libraries/class/class.
The only thing I can think of, is that you have defined a custom library path which points to a modules directory. But even then, CI should look for the library in modules/libraries/account/authentication.php.
Edit:
I just noticed you tagged your question with hmvc, which does use a modules directory.
Unfortunately, I have no experience with it.
I would (obviously) assume this what causes the behavior you described.

Zend framework Internal structure modification?

What class(FrontController , Bootstrap, Dispacher....) sets up the default structure path in ZF?
There is no single instance that has all the paths. Each component has it's own defaults, e.g. the FrontController knows that the controller directory should be named controllers, but it doesn't know how to make a full path from it (Dispatcher does it) or where to find the Action Helpers. That's defined in ActionHelper Broker. Consequently, Zend_View_Abstract holds the paths for View filters, helpers and scripts, etc.
Like #Pascal mentioned in his comment, you should not modify ZF at it's core. You will lose the changes once you update to a newer version anyway. Configure the paths through the API in your bootstrap or through the application.ini instead.
Actually it's the dispatcher's job to find the requested action controller.
So you'll have to extend either Zend_Controller_Dispatcher_Abstract or Zend_Controller_Dispatcher_Standard or even create a completely new one based on Zend_Controller_Dispatcher_Interface to fit your requirements.
But be aware that you'll have to change the way Zend_Controller_Action_Helper_ViewRenderer tries to find the required view files, too.

Adding a new view to the a Zend Framework site

We have taken on a site written in Zend framework. We didn't write the site and haven't use the Zend framework before so I'm interested in finding three things.
How do I add new views to the site, adding in a new folder to the application/views/scripts directory seems to do nothing
Are there any tutorials on how to add affiliate feeds and setups to a zend site that you can recomend?
Are there any good tutorials on learning the framework? So far all I've found is vast amounts of material that confuses me
to answer your questions in order:
You have to find the controller, that will emerge the view you want to add. There are two ways to get a view script rendered. The first one is a naming convention on the view script. The view has to be the same name as the action name of the controller. Like "indexAction" and index.phtml. The other way is to instanciate a view object within the controller and give a string with the view name at runtime. You may want to look at this excerpt:
$view = new Zend_View();
$view->a = "Hay";
$view->b = "Bee";
$view->c = "Sea";
echo $view->render('someView.php');
which I took from http://framework.zend.com/manual/en/zend.view.controllers.html#zend.view.controllers.render
I don't think that I understand what you mean by "affiliate feed"... are you talking about advertising or is it more like content syndication?
The first thing you should read may be the Zend Framework Study Guide which you find here: http://www.zend.com/community/downloads. There are many tutorials an howtos out there but, this guide is made from Zend directly and should cover the main topics.
Regards,
Mario
Are you adding the appropriate Zend_Controller as well? The Zend_Controller QuickStart is a good place to get started with MVC.
You might also look at Zend_Tool, which provides a script to help create the necessary structure.
Basically, views need corresponding controller actions. So the main index action (located, for example, in application/controllers/IndexController.php) would need a corresponding view at /application/views/scripts/index/index.phtml.
example: To reach an action within the Index Controller named fooAction() you would need a view file at /application/views/scripts/index/foo.phtml.
example: To reach the index action of contact controller you would need the controller at /application/controllers/ContactController.php (with an indexAction() inside it) and the view at /application/views/scripts/contact/index.phtml.
Getting beyond the absolute basics... view rendering can also be turned off or redirected but that is getting beyond the basics. Also if the app uses Zend_Layout there will be a layout file located somewhere like application/layouts/scripts/layout.phtml
Clear, concise and current ZF info can be strangely hard to come by. For tutorials check out:
The ZF Quickstart
Rob Allen's tutorial
Some screencasts
For adding feeds I suppose you should check out the framework's documentation for Zend_Feed

Categories