Zend framework 2 format url with parameters - php

what do I need to write in the module.config file to accept a route in the folowing format?
/action/parameter/value
I think in ZF1 this was done by default
Thank you
Here Is my route, I tried but I just can't get it to work
what route I need is classes/less/value
where less is a query parameter
'classes' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:lang/classes',
'defaults' => array(
'__NAMESPACE__' => 'Classes\Controller',
'controller' => 'Classes',
'action' => 'index',
'lang' => 'en',
),
),
'may_terminate' => true,
'child_routes' => array(
'process' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:action]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'lang' => '[a-z]{2}',
),
'defaults' => array(
),
),
),
),
),

To help you more, we will need to see your current routing.
You will basically need a simple segment route with your action, followed by an open wildcard non required section:
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/products[/:action]',
'defaults' => array(
'controller' => 'Application\Controller\Products',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'wildcard' => array(
'type' => 'Wildcard'
)
)
)

If I understand correctly you would like your route to contain a parameter. The way i updated your code below you are able to add one more constraint in your route. So it is action/less/lang. Consider the updated code below assuming i understood what you are trying to do.
'classes' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:lang/classes',
'defaults' => array(
'__NAMESPACE__' => 'Classes\Controller',
'controller' => 'Classes',
'action' => 'index',
'lang' => 'en',
),
),
'may_terminate' => true,
'child_routes' => array(
'process' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:action]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'less' => '[a-zA-Z0-9_-]*',//edit here
'lang' => '[a-z]{2}',
),
'defaults' => array(
),
),
),
),
),

Related

ZendFramework2 routing between modules

I have a SkeletonApplication installed and implemented some controllers into the standard module 'Application'.
That works fine.
But now I want to use a second module and I want to set a route from within the 'Application'-module to the new module for linking it there in a view.
The Second module is named 'Sporttabs'.
In my application.config.php I've set as found in documentation:
// This should be an array of module namespaces used in the application.
'modules' => array(
'Application',
'Sporttabs'
),
In the module 'Application' I set in module.config.php:
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'module' => 'Application',
'controller' => 'Index',
'action' => 'index',
),
),
),
'fach' => array(
'type' => 'segment',
'options' => array(
'route' => '/index[/:action][/:id]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Index',
'action' => 'index'
),
),
),
'sporttabs' => array(
'type' => 'segment',
'options' => array(
'route' => '/sporttabs[/:controller][/:action][/:id]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'module' => 'Sporttabs',
'controller' => 'Sporttab',
'action' => 'index'
),
),
),
),
),
I tried linking it within index.phtml:
Sporttabs-Projekt
This doesn't work, I only get /sporttab
Even if I try to do www.myurl.de/sporttabs I don't get into the Sporttabs-Module...
(I'm using ZendStudio to generate the ne Module, so I think all file are in right position...)
Can you give me a hint how to do this ?
There is no need to define sporttabs inside your Application config. I suggest you do this inside your Sporttabs' module.config.php file.
This is an example of my /admin route, which is a different module named Admin, which sit next to Application.
'router' => [
'routes' => [
'admin' => [
'type' => 'Literal',
'options' => [
'route' => '/admin',
'defaults' => [
'__NAMESPACE__' => 'Admin\Controller',
'controller' => 'Index',
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'default' => [
'type' => 'Segment',
'options' => [
'route' => '/[:controller[/][:action[/][:id][/page/:page][/search/:search]]]',
'constraints' => [
'controller' => '[a-zA-Z0-9_-]*',
'action' => '[a-zA-Z0-9_-]*',
'search' => '[a-zA-Z0-9_-]*',
'id' => '[0-9]+',
'page' => '[0-9]+',
],
'defaults' => [
'__NAMESPACE__' => 'Admin\Controller',
'controller' => 'Index',
'action' => 'index',
],
],
],
],
],
],
],
From there i do this:
<?=$this->url("admin/default", ['controller' => "controler_name", "action" => "action_name_from_controller", "id" => id_or_something_else_if_needed])?>
/default is there in order to have access to child routes.
#Stanimir hits the right hint for my solution:
While editing the routing for my application, I must have made a unintentional change in the order of the routes array:
The 'may_terminate' and 'child_routes' section moved to a upper level instead of being part of the 'fach'-route.
So I changed the array as follows:
'routes'=> array(
'fach'=> array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => 'true',
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/][:action[/][:id]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
),
),
),),
As an aftereffect of including the Namespace in the routing, I also had to change the controllers-array, because the alias name of the controller changed from 'Index' to 'Application\Controller\Index':
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
),
),
The same error blocked me from getting into the second module, the routes-array was misordered too.
Now the link-solution Stanimir posted in his answer works fine an I get into my new module...
Thanks for your help Stanimir!

ZF2 route with prefix in not working for default

I want add crm as prefix of CRM module.
This is router section in my module.config.php
'router' => array(
'routes' => array(
'calendar' => array(
'type' => 'segment',
'options' => array(
'route' => '/crm/calendar[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Crm\Controller\Calendar',
'action' => 'index',
),
),
),
When i use test.dev/crm/calendar/index is working properly. But it is not working for test.dev/crm/calendar . I couldn't find any issue.
When i use 'route' => '/calendar[/:action][/:id]', i can use test.dev/calendar. But i need use prefix. How can i do it?
I think it might be that you have to add 'may_terminate' => true,
So your route definition will look like this:
'calendar' => array(
'type' => 'segment',
'options' => array(
'route' => '/crm/calendar[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Crm\Controller\Calendar',
'action' => 'index',
),
),
'may_terminate' => true,
),
Try if that works.
Otherwise you can also split it and make crm to a literal route.
'crm' => array(
'type' => 'literal',
'options' => array(
'route' => '/crm',
),
'may_terminate' => false,
'child_routes' => array(
'calendar' => array(
'type' => 'segment',
'options' => array(
'route' => '/calendar[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Crm\Controller\Calendar',
'action' => 'index',
),
),
),
),
),
But this last solution means that you will always have to route to crm/calendar
As seen configs should correct.
Did you tried without last / (test.dev/crm/calendar, not test.dev/crm/calendar/)
Route configuration is correct. This route had over write from another module route. That's the issue. ZF2 has not easy way to check all routes and paths.

ZF2 Child route issues

I am trying to get some basic routing done in ZF2 but I am running into some problems.
The section that is giving me trouble is this:
'parent-categories' => array(
'type' => 'literal',
'options' => array(
'route' => '/kategorier/',
'defaults' => array(
'controller' => 'categories',
'action' => 'parent-categories',
),
),
'may_terminate' => true,
'child_routes' => array(
'child-categories' => array(
'type' => 'segment',
'options' => array(
'route' => '/kategorier[/:slug][/:parentCategoryid]/',
'constraints' => array(
'parentCategoryid' => '[0-9]+',
),
'defaults' => array(
'controller' => 'categories',
'action' => 'child-categories',
)
),
),
),
),
The original 'parent-categories' route works just fine, no problem. The problem though is the child-categories route isn't doing anything. I have the URL:
/kategorier/test-test-test-test-test/1/
but this is never matched to anything. I get the error:
The requested URL could not be matched by routing.
If I take the child-categories route out of the "child_routes" section, it always catches the request, even if the url is only /kategorier/. Can anyone see what I am doing wrong here?
A child route appends to the parent route. I.e. what you're currently matching is
/kategorier//kategorier[/:slug][/:parentCategoryid]/
Do it like this
'parent-categories' => array(
'type' => 'literal',
'options' => array(
'route' => '/kategorier',
'defaults' => array(
'controller' => 'categories',
'action' => 'parent-categories',
),
),
'may_terminate' => true,
'child_routes' => array(
'child-categories' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:slug][/:parentCategoryid]',
'constraints' => array(
'parentCategoryid' => '[0-9]+',
),
'defaults' => array(
'controller' => 'categories',
'action' => 'child-categories',
)
),
),
),
),
And i guess this should be working out alright. It's a good advice to NOT have trailing slashes, as you'd ideally always want to begin new routes with one for better readability ;)

how to pass params using a route in Zend Framework 2?

i have a router test/view and i would like to pass some params like test/view/id/123.
Im not sure how to add those params in the zf2 router.
'router' => array(
'routes' => array(
'test' => array(
'type' => 'Literal',
'options' => array(
'route' => '/test',
'defaults' => array(
'__NAMESPACE__' => 'test\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'view' => array(
'type' => 'Literal',
'options' => array(
'route' => '/view',
'defaults' => array(
'controller' => 'Index',
'action' => 'view',
),
),
),
),
),
),
),
i setup view as a child route but not sure where to add those params.
i've tried 'route' => '/view/:id/:value' and
'defaults' => array(
'controller' => 'Index',
'action' => 'view',
'id' => 'value',
)
but they don't seem to work
i am trying to understand how all this works.
any ideas? thanks
'router' => array(
'routes' => array(
'test-view' => array(
'type' => 'segment',
'options' => array(
'route' => '/test/view/:testId[/]',
'constraints' => array(
'testId' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Test\Controller\Index',
'action' => 'view'
),
),
),
In your ControllerAction you can then get the parameter "testId" by using:
$this->params('testId');
Btw: The above route gives you an url like this: /test/view/123 - I thought you may get rid of the "id" param.
If you want to create a link to one kind of this pages, you can use $this->url('test-view', array('testId' => 123)) in one of your views.

ZF2 Advanced Routing: how to have user directories?

With Zend Framework Routing (Zend\Mvc\Router\RouteStack), how can I add a module to support user profile pages.
But I'd also like to have it fall through to support other pages/modules from the root directory like /rss or /search/asdf. This is my first guess with no luck...
'Zend\Mvc\Router\RouteStack' => array(
'parameters' => array(
'routes' => array(
'default' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/[:username[/:action]]',
'constraints' => array(
'username' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\UserController',
'action' => 'index',
),
),
),
URL examples ...
https://github.com/weierophinney
https://github.com/weierophinney/following
Figured it out. Used Zend\Mvc\Router\Http\Regex matching at the very end of the Route tree ...
'user' => array(
'type' => 'Zend\Mvc\Router\Http\Regex',
'options' => array(
'regex' => '/(?<id>[a-zA-Z0-9_-]+)',
'defaults' => array(
'controller' => 'Application\Controller\IndexController',
'action' => 'user',
),
'spec' => '/%id%',
),
),
'user-schedule' => array(
'type' => 'Zend\Mvc\Router\Http\Regex',
'options' => array(
'regex' => '/(?<id>[a-zA-Z0-9_-]+)/schedule',
'defaults' => array(
'controller' => 'Application\Controller\IndexController',
'action' => 'schedule',
),
'spec' => '/%id%/schedule',
),
),

Categories