Zend Framework 2, route with query fails only if route has child routes - zend-framework2

Since 2.1.4 the Query route is deprecated, I'm routing to my blog like this:
'cro-blog' => array(
'type' => 'Literal',
'options' => array(
'route' => '/blog',
'defaults' => array(
'controller' => 'CroBlog\BlogController',
'action' => 'index',
),
),
),
And link to pages like /blog?p=x where x is the page number. This works perfectly until I add a child route. Linking to /blog still works but linking to pages give a 404 (more specific 'The requested URL could not be matched by routing.'). This is my current setup:
'cro-blog' => array(
'type' => 'Literal',
'options' => array(
'route' => '/blog',
'defaults' => array(
'controller' => 'CroBlog\BlogController',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'post' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:slug',
'constraints' => array(
'slug' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'action' => 'post',
),
),
),
),
),
I'm using a Segment child route but the same problem exists with a Literal child route.
Any way to keep the page query and the child routes?

This is solved. If anyone experiences the same issues, this is what worked for me.
Clear all cache (EdpSuperluminal, APC etc.)
Remove Zend library
Run your application without Zend library (giving loads of errors of course, but this seemed crucial to me to make it work)
Re-install Zend library
Run your application again
I installed 2.1.5dev and it fixed the issue. Wanted to make sure whether it was a bug or not so tried again with 2.1.4 and the issue was still fixed. So this is my conclusion. Cache can be a weird thing.
Edit Jumped to (the wrong) conclusions too soon. Fired up Zend Studio 10 and now it bugs again. I think Zend Studio 10 is reverting some files (not all since Query route did got marked as deprecated). Still, consider this solved, just need to find a way to tell Zend Studio 10 to use 2.1.4. Thanks!

Related

Match multiple domains with zf2 router

I need to match 2 different domains with my ZF2:
www.gamempire.it is the base
www.rankempire.it have to match to only a specific controller
So I tried adding this to my router configuration, but with no success (it match to my default controller):
'rankempire' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'www.rankempire.it',
'defaults' => array(
'controller' => 'rank',
'action' => 'index',
),
),
),
How to solve the problem?
Thanks
Solved putting the hostname route configuration to the end of the router

ZF2 Home Url maps to 0.0.0

Hi i came here since I have some troubles with my routing. This is what troubles me:
$this>url('home') maps the url to 0.0.0, since zf is generating relative paths for most of the time, so I expected it to be something like / or the complete namen taken from the $_SERVERarray
my home config entry:
'home' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[/:page][/]',
'defaults' => array(
'controller' => 'MyModule\Controller\Index',
'action' => 'index',
'page' => 1,
),
'constraints' => array(
'page' => '[0-9]+',
),
),
),
this is where I "found out" since my paginator is spitting out urls like http://0.0.0/2
anybody knows how to resolve that issue?
Running on my localhost from domain "test.com" I guess it somehow comes from that, but it's still bothersome
changed 'route' => '/[/:page][/]', to 'route' => '/[:page][/]', now it's working like a charm, i should go to bed -_-
It's a bit late adding this but i encountered another "bug" from this. As you see there is an optional page parameter and an optional trailing /, since the route which is basically mysite.com/1/ maps to // (default for page is 1) i had to remove the trailing /

404 HTTP errors not being rendered in JSON using Zend framework 2

I'm creating a simple restful api using zend framework2 and I've references Rob Allen's notes on the subject as well as this excellent tutorial by http://hounddog.github.com/blog/getting-started-with-rest-and-zend-framework-2/
below is my module_config.php. You'll see that I have routes and the JSON view strategy configured. My understanding is that when you set up the JSON strategy in this fashion it accommodates all modules. The issue is that when an invalid route is entered the 404 response is sent back in html even though the Accept header is requesting Application/json.
I've been struggling with this for a 2 days now any advice or help would be appreciated
This curl call to the api generates the expected 404 error.
curl -i -H "Accept: application/json" http://myapi-dev.local/xxx/1
Module_config.php
return array(
'controllers' => array(
'invokables' => array(
'Dips\Controller\Roles' => 'Dips\Controller\RolesController', //maps controller alias to a physical controller
),
),
'router' => array(
'routes' => array(
'dips' => array(
'type' => 'segment',
'options' => array(
'route' => '/dips/roles/:apikey/:uname/:appname',
'constraints' => array(
'apikey' => '[a-zA-Z0-9]+',
'uname' => '[a-zA-Z]+',
'appname' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Dips/Controller/Roles',
),
),
),
),
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
),
);
You would need to implement an additional rendering strategy and put it in the stack above the default 404 rendering strategy. Alternatively, you could extend the existing ViewJsonStrategy to include error handling.
The default 404 Strategy is Zend/Mvc/View/Http/RouteNotFoundStrategy. If you look at it's detectNotFoundError method, you can see when it gets triggered.

404 error, Zend Framework 2 The requested URL could not be matched by routing

I'm getting:
A 404 error occurred
Page not found. The requested URL could not be matched by routing.
My module.config.php file is:
'router' => array(
'router' => array(
'Test' => array(
'type' => 'Segment',
'options' => array(
//http://localhost/Test/Test
'route' => '/Test[/[:action]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Test\Controller\Test',
'action' => 'Test'
),
),
),
),
),
help please, i am new in Zend Framework 2 !
You should use configuration like Application module in ZendSkeletonApplication:
'router' => array(
'routes' => array(
'test' => array(
'type' => 'Literal',
'options' => array(
'route' => '/book',
'defaults' => array(
'__NAMESPACE__' => 'Test\Controller',
'controller' => 'Test',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'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(
),
),
),
),
),
),
),
You just add the following code:
'test' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
),
'defaults' => array(
'__NAMESPACE__' => 'Test\Controller',
'controller' => 'Test',
'action' => 'index',
),
),
),
add this code to 'child-routes' key and after that you'll access to url: localhost/:controller_name/:action_name/:id (example : http://zf.dev/test/index or http://zf.dev/test/add/1). And now it's work!
This code can fix error 404 for tutorial in zf2 documentation.
You have a typo, try this:
'router' => array(
'routes' => array(
Routes rather than router twice..
Achieve fix it, I was missing the letter "d", was thus: Zend \ Loader \ StandarAutoloader I added the "d": Zend \ Loader \ StandardAutoloader. Greetings Friends. TIP: Zend Studio 10 and his version de ZF2 run perfect for this moment !
Please check the .htaccess file and index.php files. If these are exist in public folder means, you have to use the url as
http://localhost/public/Test/Test.
Your codes are almost right. Andrew has guided you well. Let me know your response.
1.You should check also application.config.php
and add your module name into RETURN array.
return array(
'modules' => array(
'Application',
'your_module',
.....
),
2.If Doesn't.Check route array in module.config.php
I will suggest also check the data folder that has cached config files, config files being cached in dev install also may cause this issue.
delete files inside data/cache and try.
PS: if you are just starting try with blog module on zend site it is for beginners and more updated with new versions.
https://framework.zend.com/manual/2.4/en/in-depth-guide/first-module.html

How to select controller namespace with Zend Framework 2 routing?

I have this as part of my DI config, from the skeleton applicaiton:
'routes' => array(
'default' => array(
'type' => 'Zend\Mvc\Router\Http\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(
'controller' => 'Application\Controller\IndexController',
'action' => 'index',
),
),
),
I would like to make the following routings:
http://me.com/mycontroller/myaction -->
controller=Applicaiton\Controller\Mycontroller
method=myactionAction
However, the above config produces:
http://me.com/mycontroller/myaction -->
controller=Mycontroller
method=myactionAction
As you can see, the namespace for the controller is missing. Where/how do I put the namespace in? (I know I could make a DI alias for every controller, but that would deafeat the purpose of having segment matching.)
It is perfectly possible to achieve what you were originally trying to achieve. In your defaults, you could have added a __NAMESPACE__ key and slightly altered the controller key.
You could have done this:
'default' => array(
'type' => 'Zend\Mvc\Router\Http\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(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
),
The only other change that would be needed is when you register your controller as an invokable in your module configuration you would have to do it like so:
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
),
),
Take note to how I used Index in the key instead of IndexController.
This __NAMESPACE__ key of the defaults definition of a route and the removing of "Controller" at the end of the key in the invokables array behavior isn't mentioned anywhere in the documentation that I could find. I actually gleaned this information from how the routing of the ZendSkeletionApplication works:
https://github.com/zendframework/ZendSkeletonApplication/blob/2ce1cf0dd40046024970d87d3998e16cde41c7db/module/Application/config/module.config.php
You should not use segments for controllers in your routes. Segment matching is possible for actions and other parameters, but not for controllers. You might use aliases to help mycontroller match MyNamespace\Mycontroller, but it is not recommended. The best, easiest and with the most performance way is to have routes for every controller, use segments for actions if necessary.

Resources