Match multiple domains with zf2 router - zend-framework2

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

Related

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!

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

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