Unable to route with module in ZF2 - php

I've created a module, a basic copy of the the albums example given in the ZF2 documentation, however, with the new module, I am not able to access it at all - I'm always given a 404 error. I'm building this on the ZF2 skeleton.
I've got three modules loaded: Application, Frontend and Security.
Both Frontend and Security are duplicates of each other, however, I have thoroughly checked and there is no reference to old code (as I literally copied the module folder and renamed/rewrote references).
The module is also loaded in application.config.php.
Any ideas on what I'm missing?
Module Config:
return array(
'controllers' => array(
'invokables' => array(
'Security\Controller\Security' => 'Security\Controller\SecurityController',
),
),
'router' => array(
'routes' => array(
'security' => array(
'type' => 'segment',
'options' => array(
'route' => '/security[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Security\Controller\Security',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'security' => __DIR__ . '/../view',
),
),
);

I had the same problem while following the skeleton application tutorial (Getting started: A skeleton application). Whenever I would go to the album url in the browser (ZendSkeletonApplication/public/album in my case), I would get a 404 error page but no details on why I got the 404. It wasn't clear to me how I would be able determine why I was getting the 404 when I had double checked everything and was pretty sure I copied and configured the Album module properly. It turned out that I was missing a slash in my route (module.config.php). For example I had 'route' => 'album[/:action][/:id]' instead of 'route' => '/album[/:action][/:id]'.
I was only able to figure it out by intentionally causing errors by misspelling things like making the 'Album\Controller\Albums' instead of 'Album\Controller\Album'in the invokables value, this would cause a stack trace to display which then showed the ZF2 classes that where called on the request. I would continue to misspell, test, and then correct each part of the module.config.php until I was given a clue to what part of the configuration was causing the error.
I'm pretty sure this was not the best way to debug an application's configuration.

There is few things that need to be make sure is:-
You have to add your module in
application.config.php (which you are saying you done it.)
Security\Controller\Security has to be same in default too (which you already has)
One more thing is Your folder structure....
-
Just to doulbe check you have a /MODULE/src/MODULE/Controller/CONTROLLER_FILE_NAME.php
I hope that helps..

I know it is an old post. However another thing to make sure you have in the modules top directory (same directory as the Module.php file) is the "autoload_classmap.php"
file with "<?php return array();?>" inside of it.

A simple tip to know whether your rule has already added correctly to the routes or not, you may check the routes value in the config file inside any working module, as following:
$config = $this->serviceLocator->get('config');
var_dump($config);

Related

ZEND zf2 trying to debug controller not found

I am getting a 404 for controller not found and I am having difficulty debugging it.
Where would I debug in Zend zf2 to see what controller it is actually looking for?
The error is:
A 404 error occurred
Page not found.
The requested controller could not be mapped to an existing controller class.
Controller:
not-found(resolves to invalid controller class or alias: not-found)
No Exception available
So my question is not for you to find my problem but to help m3 find where in ZEND I can put breakpoints - help me learn to debug what ZEND is doing.
Between the service manager and listeners and autoloaders, and factories etc... I find ZEND so complex that I can't figure out where to put a breakpoint and see what is happening - in this case what controller is it trying to open.
"Teach a man to fish, rather than feed the man fish".
Thanks
For that problem i don't think you can use breakpoint. Because it gives error before action starts, it couldn't found the controller.
My solution for that when i face with that problem.
Check controller file if it has the correct name in both file and class definition and namespace.
Check module.config file for routing and controller definition.
If you are not using autoloader, check classmap file.
In one of your routes you are using a controller which is not defined under the 'controllers' key in your module.config.php
For example in this route there is a controller 'Application\Controller\Index':
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
And you have to create a controller entry:
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),

Doctrine Resolve_target_entity in config

I found this on the doctrine website page:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/resolve-target-entity-listener.html
A way to let my entity communicate with an interface which then can be configurable. The only problem is that i cant find anywhere how to put it in my array config. I already checked the configuration source but there is nothing in the docs:
https://github.com/doctrine/DoctrineORMModule/blob/master/docs/configuration.md
Hope someone can help
Thanks
You can use it like this:
'doctrine' => array(
'entity_resolver' => array(
'orm_default' => array(
'resolvers' => array(
'MyModule\Entity\FooInterface' => 'OtherModule\Entity\Foo',
),
),
),
We use it e.g. here (as a live example) in Soflomo\Blog.

zend framework 2 default routing action change url to default action

i thought i had the routing under control until i found out that i have now clue of how to have the default action url change, that means:
when ever a user enters a url, for example:
http://www.mysite.com/form/myform
the routing will always redirect him to the default action "show" (one of many actions this controller has):
http://www.mysite.com/form/myform/show
but my url remains the same (with the "show" action):
http://www.mysite.com/form/myform
what am i missing here?
'routes' => array(
'form' => array(
'type' => 'segment',
'options' => array(
'route' => '/form[/:form[/:action]]',
'constraints' => array(
'form' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Form\Controller\Form',
'action' => 'show',
),
),
),
),
thanks!
edited note:
i have noticed (thanks to #codeHeart marks) that i had some mistakes/misclarification trying to explain the problem, so i edited the main question examples.
thanks again all!
Looking at this in your question/
http://www.mysite.com/controller
and your router configuration I expect that should be throwing a 404 error, as there is no matching route in your config that matches /controller(If there are more routes other than the one that you show in you question, let us know or maybe you mistyped this url).
Apart from above,
your route tells to do the following:-
if url is
http://yoursite/form OR
http://yoursite/form/controller
go to the controller action that you have mentioned in the defaults, as the route was matched to the url but didn't get matched completely to an action so going to the default.
A non existing controller or a non existing action should be throwing 404. e.g
http://yoursite/form/controller/non-existing-action
Even this url
http://yoursite/form/
won't get matched according to your route and will be throwing a 404.
And for your question if you want to change the default action to something other then the "show" action, simply change the action parameter/key in defaults
'defaults' => array(
'controller' => 'Form\Controller\Form',
'action' => 'some-other-action',
),
Hope this helps and sorry if I was of no help.

Creating new Module in Zend Studio (Zend Framework 2)

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

How do I configure Dependency Injection in Zend Framework 2?

DISCLAIMER: I'm a complete noob to Zend.
I'm evaluating Zend Framework 2 at work, and trying to configure it to work with ZfTwig for templating. (See here: https://github.com/mtymek/ZfTwig)
I got through Step 3 of the config ok, but I can't figure out Step 4.
I tried placing the following in application.config, but no good.
Where am I supposed to put this?
return array(
'di' => array(
'instance' => array(
// setup other stuff...
// ...
// setup view script resolvers - very similar to configuration
// from ZendSkeletonApplication
'Zend\View\Resolver\AggregateResolver' => array(
'injections' => array(
'Zend\View\Resolver\TemplateMapResolver',
'ZfTwig\TemplatePathStack',
),
),
'Zend\View\Resolver\TemplateMapResolver' => array(
'parameters' => array(
'map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.twig',
),
),
),
'ZfTwig\TemplatePathStack' => array(
'parameters' => array(
'paths' => array(
'application' => __DIR__ . '/../view',
),
),
),
// Tell TwigRenderer how it should locate .twig files
'ZfTwig\TwigRenderer' => array(
'parameters' => array(
'resolver' => 'Zend\View\Resolver\AggregateResolver',
),
),
),
);
Google is no help... I can't find any documentation on Zend's site or anywhere telling me where this is supposed to go.
Thanks for the help!
The di configuration is from the first betas of Zend Framework 2. Zend\Di is a component still available, but internally (as with many other modules) replaced by Zend\ServiceManager.
Basically, both are able to provide dependency injection. Only for Zend\Di it can do this kind-of automatically and for Zend\ServiceManager there are other options to make dependency injection more explicit.
To give an answer to your question: ZfcTwig is now part of ZF-Commons and https://github.com/ZF-Commons/ZfcTwig is the location you have to search for now. Just for your insights, this file is an example of a factory used by the service manager. For more background of service managers in Zend Framework 2, I have written a blog post two months ago which might be interesting.

Categories