I'm learning ZF2 and I am constructing a web site to make sure that I understand the new concepts of ZF2, but I was wondering if there is a way to specify a template for the error pages in the application module.
Thanks
There some pre-set configuration options within the module.config.php like:
...
'view_manager' => array(
'not_found_template' => 'error/404',
'exception_template' => 'error/exception',
'template_map' => array(
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/exception' => __DIR__ . '/../view/error/exception.phtml',
),
),
...
After creating the Folder and the Files you should be good to go.
Related
I would like to structure my project like the following:
MyModule/src/MyModule/Controller/TestController.php
MyModule/src/MyModule/Controller/Admin/TestController.php
The problem is, that both controllers look for the view file mymodule/test.phtml, because the directory Admin doesn't matter. Is there a way to take care of the directory without write it down manually in each controller action?
I would like to structure it like that:
MyModule/src/MyModule/Controller/TestController.php => view/mymodule/test.phtml
MyModule/src/MyModule/Controller/Admin/TestController.php => view/mymodule/admin/test.phtml
Or maybe someone has an idea to structure it otherwise?
Thanks
I've seen this done in a previous project before. Pretty sure you can config this in the module.config.php file, maybe something like:
// View file paths
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'MyModule/Admin/Test' => __DIR__ . '/../view/mymodule/admin/test.phtml'
)
)
Just make sure the paths are correct. However, you could just have an Admin module and have all of the admin controllers reside there that way you wouldn't have to worry about the Controller/Admin issue you are having with the view files right now. Then all of your other modules could either extend, use DI or the service mgr to get what you need from the Admin module.
You can override the default template injector with your own one where you will specify the logic that will resolve the template path for the admin controllers.
Check out my blog post covering this topic in depth with an example
http://blog.igorvorobiov.com/2014/10/18/creating-a-custom-template-injector-to-deal-with-sub-namespaces-in-zend-framework-2/
I've been able to load a view template and use it to create the body of an email message. The code is similar to the answer here:
How to render a mail template with layout in ZF2?
But now I'm looking to take this code and move it into a module that helps the rest of my application send emails. I'm thinking I would like to make it as easy/transparent as possible to use views for all emails.
So the basic question is, how can I setup the code to accept the information it needs to render a template and send the email?
I already have my transport information held inside the service locator, and sending email works fine. My primary concern is mostly the messy code needed for the template resolver:
$view = new PhpRenderer();
$resolver = new TemplateMapResolver();
$resolver->setMap(array(
'mail' => __DIR__ . '/../../../view/communication/email/new-project.phtml'
));
$view->setResolver($resolver);
I am alright with having all the *.phtml for emails held inside the new module. There's no need to send adhoc emails out of the application (we use communications internal to the application).
Is there a way to use the module.config.php 'view_manager' => 'template_map' and bypass the need to create a new resolver()? In that case I could setup the email code to accept the named template that should be in that map.
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
You simply have to access the default viewrenderer via the ServiceManager.
$viewRenderer = $this->getServiceLocator()->get('viewrenderer');
$mailView = new ViewModel();
$mailView->setTemplate('my-namespace/controller/mailtemplate.phtml');
$mailView->setVariables(array(
//k=>v paired data
));
$renderedOutput = $viewRenderer->render($mailView);
$mail->setBody($renderedOutput);
And that's pretty much it. Obviously though the concrete mail handling is stripped from this example, as you're mostly looking at another problem, given your description.
I have been following the tutorial from Zend (http://www.youtube.com/watch?feature=player_embedded&v=EerB9bTvqrY) however when I add a new Module in my project I can not navigate to it, are the instructions in this tutorial incorrect?
Basically when I add a new Module in Zend Studio to my Zend Framework project I can not navigate to it, my new module is called "deals". I navigate to localhost/dealproject/deals and I get error 404. When navigating to localhost/dealproject/ it loads the zend skeleton application page correctly.
Thanks for your help.
module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'Deals\Controller\Deals' => 'Deals\Controller\DealsController',
),
),
'router' => array(
'routes' => array(
'deals' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/deals',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Deals\Controller',
'controller' => 'Deals',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
// specific routes.
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'Deals' => __DIR__ . '/../view',
),
),
);
Make sure you've enabled the module in ~/config/application.config.php
'modules' => array(
'Application',
'Deals',
),
To me the route configuration looks correct and it should show results. What I noticed is that your base URL (localhost/dealproject/) for the ZF2 application is in a sub directory of the domain's root. As you probably know all the requests are mapped to the index.php of your application (~/public/index.php). This is done by some configuration in your .htaccess file (in the same directory).
In the example URL www.example.com/blog if blog doesn't exists as a folder under www.example.com's root you get a 404 Page not found. However, the .htaccess of ZF2 makes apache call the index.php in the domain's root if no directory is found (this assumes you use the ZendSkeletonApplication).
Now, since your index.php is not in the domain root (localhost/) but in localhost/dealproject/ and your .htaccess is (I assume) in localhost/dealproject/ as well, when you call localhost/dealproject/deals apache is looking for a directory dealproject/deals since in localhost there is no .htaccess to prepare the necessary configuration.
I would advise you to make dealproject folder the root of localhost. This will enable you to call your route like this: localhost/deals and .htaccess and index.php will be processed correctly.
Hope this helps :)
Stoyan
I need 2 different template maps in ZF2 , one for admin and oen for front-end, currently from what I can see ZF2 merges the 2 module.config.php files that are used in the 2 modules I configured, and causes the template map I need to set for the admin, to be loaded in front module also.
the /Application module.config.php
...
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
...
the /admin module.config.php
...
'view_manager' => array(
'template_path_stack' => array(
'admin' => __DIR__ . '/../view',
),
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
),
),
...
what should I modify so that i can load separate "view_manager" arrays for the 2 separate modules ?
It would be great if you can explain why you are trying to achieve this. As i can see you are trying to have a different layout for admin. Maybe you want to take a look at this module which can already do what you are trying https://github.com/zf-commons/zfcadmin. This module has a layut setup for the admin route.
I too had this problem. I took the approach which is proposed in the below module
https://github.com/EvanDotPro/EdpModuleLayouts
I'm new to ZF2 and I too was looking for an answer on how to have a completely different template for a dashboard, admin and front end.
I used EdpModuleLayouts as suggested here and in many other posts. This solved one aspect of the problem. The layout. I was now able to provide different layouts for the same template which would work well if I were using the same template or wanted to prove a different layout for say forgotten password, registration or a login. But I didn't. I needed a whole different set of folders, css files etc. At this point I could have just nested all templates in to a template folder and pointed the links in the layout files to the appropriate folders. But I didn't want this either.
I also included the zfc-admin module into my app which gave me a clue as to the other aspect which is to provide a different source directly for files. (Uninstalled afterward)
So adding by adding the following to my module_name/config/module.config.php
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view'
),
),
Enabling EdpModuleLayouts in application.config.php and adding the code below to the Application module.config.php
'module_layouts' => array(
'Application' => 'layout/layout',
'Dashboard' => 'layout/dashboard',
'Admin' => 'layout/admin',
),
This is probably not the best way to do it but it worked. The only issue I could really see with doing it this way is that EdpModuleLayouts wants to pull all the layouts from the Application/view/layout folder. It did however allow me to keep all my module template files in the view section of the module being worked on.
i have a bare bones zf2 application and i have 2 modules (Users and Album).
They both have their own layouts but i want to use the layout from Album for the Users, just to keep them a bit more consistent.
Im not sure what is the best way to delegate what layout a module should use.. from the controller __contruct, or on each individual action or in the module.config.php.
maybe here:
'template_map' => array(
'layout/top_nav' => __DIR__ . '/../view/layout/top_nav.phtml',
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
^^ probably i should set this path to the layout i want to use ^^
'posts/index/index' => __DIR__ . '/../view/posts/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
any ideas? maybe the best practices?
thanks
edit: i just did this and it worked:
'layout/layout' => __DIR__ . '/../../album /view/layout/layout.phtml',
but i'm still not sure if this is best practice.
maybe i can setup multiple template_map arrays and use them in the User controller, in my case
Edit: removed my own bullshit, just see the alternative approach :) That works
Alternative approach
Evan Coury also made the effor to have the Layout configuration globally available. This is especially true for single developers who won't make their modules public, ever. You will find his EdpModuleLayouts Module right here.
Using that module switching layouts becomes even simpler like the following:
<?php
array(
'module_layouts' => array(
'ModuleName' => 'layout/some-layout',
),
);
Hope it helps.