Check what view file ZF2 is trying to load? - php

I am building my first ZF2 application, and in one of my modules, the views associated with my controller are not loading. Is there a way I can check what view path ZF is trying to execute?
I have checked all of the file paths and module config settings, and they seem correct, and all of my other modules that have the same layout work fine, so I am thinking this is either a filename or namespace issue.

If you use Zend-developer-tools it shows you in the toolbar which layout, template is used.
To your problem - in module you can replace the template from other modules. But it should depend on template path stack.
In module.config.php, you should have something like:
'view_manager' => array(
'template_path_stack' => array(
'application' => __DIR__ . '/../view',
),

Related

loading classes from main vendor directory from a zf2 module

I'm writing a PHP application with Zend Framework 2.3.1.
I wrote a ZF2 module that I placed in vendor directory. vendor/TuxDrink.
That module requires another module that I placed in vendor/ServiceLocatorFactory (https://github.com/fezfez/ServiceLocatorFactory).
now.. I want to use the ServiceLocatorFactory Module inside my module. but that ServiceLocatorFactory directory is in the vendor directory of the application, not of my specific module.
my TuxDrink Module.php
<?php
namespace TuxDrink;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
class Module implements AutoloaderProviderInterface
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
what exactly do I need to add in order for my TuxDrink module to detect the ServerLocatorFactory module and allow me to use it's classes.
for now I get
Fatal error: Class 'ServiceLocatorFactory' not found
any information regarding the issue would be greatly appreciated.
A couple of things/best-practices to consider:
1) If it's vendor/, it gets put there by composer. That way, all the autoloading is configured properly. Also, .gitignore typically ignores everything in vendor/, so keep that in mind.
2) That 'ServiceLocatorFactory' module is a very bad idea. The best practice is properly inject real dependencies - . A module whose purpose is to expose the ServiceManager globally is an anti-pattern. You'll never know what dependencies your classes have, because any of them can pull any dependency from anywhere.
It doesn't matter where the class is located as long is it can be autoloaded. Since the modules are in vendor/ I'm assuming you installed them using Composer (if not, you should).
Other than that, remember that the class is named ServiceLocatorFactory\ServiceLocatorFactory, not ServiceLocatorFactory which is what is shown in your error. (This may be the only issue.)
I'm also curious why you feel the need to have a module that makes the service manager a global

ZF2 Default Routing Configuration

I am having trouble with the workflow when creating new modules/controllers to the ZF2 Skeleton Application.
I created a new module test and navigated to mydomain/test. This returns a 404 error until I do the following:
Define my Module in the global config file
Define my route in my module config file
Define my Controller as an invokables in the module config file
Define the path of my view as a view_manager in the module Config file
I am new to ZF2 and trying to better understand the workflow for Application development. This seems like a very cumbersome way to develop, as there is so much configuration needed.
(Rapid application Development??)
Is there a Default means of defining literal routes, controllers, and view rendering in ZF2?
Create your module with zftool, it will add it to your global config.
You will have to create at least one route for each module, take a look at the route in the Application module with this route you're covering allot the comment says:
The following is a route to simplify getting started creating new
controllers and actions without needing to create a new module. Simply
drop new controllers in, and you can access them using the path
/application/:controller/:action
You will have to add controller to the invokables
Use template_path_stack:
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
)
i use template_path_stack during development and template_map in production

How to add a rule to the Zend URL mapping and compose multiple MVC paths?

This is because I want to develop a web platform with more than one application in the same project.
In any MVC web application we should have this default URL schema:
domain/controller/action/parameters
1: In Zend, what can I do (in which files) to change this schema to add the application name before the controller name?
Expected Result: domain/application/controller/action/parameters
2: How can I implement the consequences of this new URL block in terms that I will separate the MVC for each application, maintaining the shared resources in a separate directory? What changes may I do in Zend autoloader
Expected Result:
/public_html/
/public_html/platform
/public_html/platform/apps
/public_html/platform/apps/base (user interface container)
/public_html/platform/apps/crm
/public_html/platform/apps/crm/model
/public_html/platform/apps/crm/view
/public_html/platform/apps/crm/control
/public_html/platform/apps/crm/public
/public_html/platform/apps/crm/public/css (and etc.)
/public_html/platform/apps/erp
/public_html/platform/apps/erp/model
/public_html/platform/apps/erp/view
/public_html/platform/apps/erp/control
/public_html/platform/apps/erp/public
/public_html/platform/apps/erp/public/js (and etc.)
/public_html/platform/sys
/public_html/platform/sys/core
/public_html/platform/sys/cfg
/public_html/platform/libs/
/public_html/platform/libs/zend
/public_html/platform/libs/template_it
/public_html/platform/libs/custom
i think it is as easy as having actual different ZF2 application, every one in its own folder, and in the same level, a "vendor" folder where you put all the shared structure (coming from zend, third party libraries, etc).
Then inside the vendor folder, i would create another folder for your own shared code, including all your modules that has to be used by more than one of the applications, so your code is a library for yourself.
Since your app is actually in domain/application, and everyone has it own config, it is very straightforward to have domain/application/controller/action/parameters routes: you just create your normal controller/action/parameters routes, since the app actually resides in domain/application/ and the router doesnt have to care about it.
As you noticed, another problem is the autoloader. YOu just need to update the references to the shared modules inside your application.config.php for everyone of your apps
return array(
'modules' => array( //....
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php'
),
'config_cache_enabled' => false,
'cache_dir' => 'data/cache',
'module_paths' => array(
'./module',
'../vendor',//reference to your library modules
),
),
//...
);
of course, if the modules doesnt reside directly inside vendor/module but something like vendor/libraryname/module, you have to take a look at your autoload system (Composer autoloading or whatever) and add the classes or namespaces to the corresponding maps.

Zend Framework 2 - Internal server error in module

I just created a module with a submodule like Backend\Entity1. I checked every path and namespace, they all seem to be ok.
I took out codeblocks one by one but stil I just get via Firebug a
"NetworkError: 500 Internal Server Error"
Any ideas what I can do to find the error?
I just don't know what might be wrong. Honestly, why does the framework explain itself what is happening? Its just fishing in the dark now...
BTW: The error.log doesn't show something either...
Edit:
I'm guessing that this error is raised because my app can't find the phtml files. Maybe I'm wrong but usually when I get this kind of silly error it has something to do with missing files...
My config code looks like this:
'view_manager' => array(
'template_path_stack' => array(
'entity' => __DIR__ . '/../view',
),
),
My namespace is MasterData\Entity and my add/edit/delete/index.phtml lie under view\masterdata\entity.
Any ideas how I can tell the TemplatePathStack how to look in this dir for my files?
If your want your sub module to be accessed create proper route in the parent module to make a way to access your sub module.
I IMHO if you need to create sub modules, create as module and place the modules under modules folder of your application, if you have components or third party modules place it under vendor folder.
This would be easier to manage at later stages an your url will also be much simpler
In my case, I created the new model in another way but I had the same error when I tried to see the result in the explorer. The problem (in my situation) was the access to files from the explorer, It resolves by changing files's permissions in linux's terminal.

URL cannot be found on the server for my FuelPHP Module

I'm creating a small FuelPHP application that contains a blog module. In this module the posts controller just assigns a simple view to the template for the posts index.
I have read through the FuelPHP docs and have the following settings in the app config for the modules:
'module_paths' => array(
APPPATH.'modules'.DS,
APPPATH.'..'.DS.'globalmods'.DS
),
'always_load' => array(
'packages' => array(
'auth',
'orm',
),
'modules' => array(
'admin',
'blog',
),
),
The application itself is outside my WAMP www docroot but the assets, htaccess and index.php are inside.
I have no idea why the server cannot find the localhost/blog/posts/index URL as I have followed everything advised on the docs and the homepage (root route) seems to display fine. It is only when I click the blog link (blog/posts/index) that it states
"Not Found
The requested URL /blog/posts/index was not found on this server."
Any help would be much appreciated!
I had the same problem myself. Two key things I did were:
1) Start with a fresh copy of Fuel. I was missing some sort of configuration file, so I create a brand new Fuel framework, and then added all the app files back in
2) Enabling the 'rewrite_module' in the Apache Modules (in the Wampserver config)
Try these two things and see what you get.

Categories