ZF2/3 catchall route with slug - zend-framework2

I need one route to fetch page content out of my CMS by a slug. I thought I could easy create a configuration like the following but it seems that it only works with multiple parts in the route. My plan was to create a custom route, but if I don't get a match with the system routes I don't think I will get one with my custom route :)
So is there a way to do that?
Does not work
'router' => [
'routes' => [
'cms' => [
'type' => \Zend\Router\Http\Segment::class,
'options' => [
'route' => '/:slug',
'defaults' => [
'controller' => \Cms\Controller\PageController::class,
'action' => 'index'
]
]
]
]
];
Works
'router' => [
'routes' => [
'cms' => [
'type' => \Zend\Router\Http\Segment::class,
'options' => [
'route' => '/test/:slug', // <-- Here is the change
'defaults' => [
'controller' => \Cms\Controller\PageController::class,
'action' => 'index'
]
]
]
]
];

OK, forgett everything... I already had a segment route with the same format, but with some constraints. Therefore I got a 404 page. So everything works fine...

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,
),

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

Unable to override route via DI

I'm attempting to override the routes provided by ZfcUser using akrabat's method, however it doesn't seem to have any effect. I'm starting by rebasing the routes from /user to /users but will want to add other routes later on (should be simple if I can get this working).
My DI configuration:
return [
'di' => [
'instance' => [
'Zend\Mvc\Router\RouteStack' => [
'parameters' => [
'routes' => [
'zfcuser' => [
'options' => [
'route' => '/users'
]
]
]
]
]
]
]
];
Am I making an obvious mistake or has the configuration structure for this changed since this blogpost?
You'd overwrite configuration from within your own modules.
'router' => array(
'routes' => array(
'zfcuser' => array(
//...
)
)
)
Though if i'm correct the full DI Path should still work... It's important, too, that your module loads AFTER the zfcUser-Module. So within your application.config.php be sure that your modules Namespace is listed after zfcUser. Since the arrays simply get overwritten depending on load time ^^

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