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!
Related
I'm creating a new small application with Zend Framework 2 and I've some problem with routes...
I've created a a new module cloning the Skeleton Module with Git and renaming it "Users". I've added controllers to register a new user, to login and to perform CRUD operations.
This is my module folder structure:
and this is my module.config.php file:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Users\Controller\Index' => 'Users\Controller\IndexController',
'Users\Controller\Register' => 'Users\Controller\RegisterController',
'Users\Controller\Login' => 'Users\Controller\LoginController',
'Users\Controller\UserManager' => 'Users\Controller\UserManagerController',
),
),
'router' => array(
'routes' => array(
'users' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/users',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Users\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
// specific routes.
'login' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/login[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\Login',
'action' => 'index',
),
),
),
'register' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/register[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\Register',
'action' => 'index',
),
),
),
'user-manager' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/user-manager[/:action[/:id]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\UserManager',
'action' => 'index',
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'users' => __DIR__ . '/../view',
),
),
);
When I try with my browser the url test.local/users/login or test.local/users/register everything works fine but if I try test.local/users/user-manager I get the following error:
Route with name "user-manager" not found
whilst with test.local/users/user-manager/edit/5 it is rendered the correct page.
I'm bit confused and I don't know how to solve this problem.
Any help is very appreciated. Thanks in advance.
problem solved! #AlexP was right, the problem was in index.phtl file with $this->url('user-manager');, I've changed in $this->url('users/user-manager') and now it works.
Thanks again!
I think the issue is with constraints and default values. Try to remove constraints in user-manager route and see what happen.
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.
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(
),
),
),
),
),
Hi i'm trying to set two modules to work togheter. One is responsible for the front pages (Front module) and the other for the admin pages (Admin module).
I have configured the routing of the two modules normally and everything is working fine when i test them separatly. Once I try to make them work together I have some routing problems.
For example when i try to go to the link : localhost.com/en/admin/index/add the framework uses the front module routing configuration and not the admin.
So basically it tries to go to:
Module - "Front",
Lang - "en",
Controller - "Admin",
Action - "Index"
But what I need is :
Module - "Admin",
Lang - "en",
Controller - "Index",
Action - "add"
Param - "none"
So the need is to tell to the front configuration routing not to access the links that have the admin part in them but I can't get it to work. Here is my front module routing:
'application' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:lang][/:controller[/:action[/:id]]]',
'constraints' => array(
//'controller' => '^((?!admin).)*[a-zA-Z][a-zA-Z0-9_-]*',
'lang' => '[a-zA-Z]{2}',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
//'lang' => 'en',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:lang][/:controller[/:action[/:id]]]',
'constraints' => array(
'lang' => '[a-zA-Z]{2}',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
),
),
),
),
),
and the admin routing code :
'admin' => array(
'type' => 'Segment',
'options' => array(
'route' => '/admin[/:lang][/:controller[/:action[/:id]]]',
'constraints' => array(
//'controller' => '^((?!admin).)*[a-zA-Z][a-zA-Z0-9_-]*',
'lang' => '[a-zA-Z]{2}',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'Admin\Controller',
'lang' => 'en',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:lang][/:controller[/:action[/:id]]]',
'constraints' => array(
'lang' => '[a-zA-Z]{2}',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
),
),
),
),
),
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.