ZF2 Home Url maps to 0.0.0 - zend-framework2

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 /

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 - Navigation routing with language

In ZF2 I have two languages English and Chinese. Every route starts with language like:
'about' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/[:lang]/about',
'constraints' => array(
'lang' => '(en|zh)?',
),
'defaults' => array(
'controller' => 'Application\Controller\About',
'action' => 'index',
'lang' => 'en',
),
),
),
I have already translated label with setTranslator, but I do not know how I can add language parameter to the route. It seems to me like this problem Translation of URI segments in ZF2
'navigation' => array(
'default' => array(
array(
'label' => 'About',
'route' => 'about',
'class' => 'top-level',
),
),
Or is there better way how to handle this for example prepend language to every url like this How to prepend language to every url in Zend Framework
In general, there is a params key in the navigation to supply route parameters. It must contain an array of all values used in the route.
An example, for the route foo[/:bar] you can have this navigation configuration:
array(
'label' => 'Foo',
'route' => 'foo-bar',
'params' => array('bar => 'baz'),
),
In your translation case, provide a params key called lang and than one will be used.
However you probably want to use the route match parameter for the language. If you are on the page from language zh then the parameter is automatically zh. Then you can use the use_route_match.
array(
'label' => 'About',
'route' => 'about',
'use_route_match' => true,
),

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

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!

Zend\Navigation breadcrumb doesn't find active elements

i've got my navigation running and all links are working fine. Rendering the Navigation using $this->navigation($nav)->menu() will display an unordered list and all links are working.
Additionally the active link there is working, too. The active element has class="active" as an attribute.
Rendering the same navigation, only as a bradcrumb $this->navigation($nav)->breadcrumbs() doesn't render me anything. This MAY be due to my navigation only being one level deep for now, but imo the first level should still be rendered.
Using super modern die()-debugging i found out that nothing get's rendered because the findActive() of the viewHelper doesn't find an active element and therefore returns an empty string.
Any thoughts on where my error may be located? Any insight will be greatly appreciated. Here's my code so far:
'navigation' => array(
'default' => array(
'biete' => array(
'label' => 'Biete',
'route' => 'biete',
),
'suche' => array(
'label' => 'Suche',
'route' => 'suche',
),
'administration' => array(
'label' => 'Administration',
'route' => 'admin'
),
'dashboard' => array(
'label' => 'Meine Artikel',
'route' => 'dashboard'
),
'login' => array(
'label' => 'Anmelden',
'route' => 'duituser/login'
),
'logout' => array(
'label' => 'Abmelden',
'route' => 'duituser/logout'
)
),
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
And the view Parts located in my layout.phtml
<?php echo $this->navigation('navigation')->menu(); ?>
<?php echo $this->navigation('navigation')->breadcrumbs(); ?>
Thanks again in advance.
I had a play with the Navigation classes/helper - it seems that breadcrumbs are rendered (in the example below) when the 'playground' route is matched. However by adding 'active' => true to my default route means it will always be rendered by the breadcrumb helper.
'navigation' => array(
'default' => array(
'test' => array(
'label' => 'Home',
'route' => 'test',
'active' => true,
'pages' => array(
'playground' => array(
'label' => 'Playground',
'route' => 'playground',
),
),
),
),
),
In addition to the answer of DrBeza, here is what helped me. The problem was that the Breadcrumb-Navigation only got rendered once we hit the second level. But you can obviously change that setting by calling the following:
$this->navigation('navigation')->breadcrumbs()->setMinDepth(0);
To me, this is all that was needed. As DrBeza pointed out however in some cases this may not be enough.
In my case helps only this this:
$this->navigation('navigation')->breadcrumbs()->render('navigation');
The strange is, that it's required only if $this->navigation('navigation')->menu() is called before, because breadcrumbs are not rendered.

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