[英]ZendFramework2 routing between modules
I have a SkeletonApplication installed and implemented some controllers into the standard module 'Application'. 我安装了SkeletonApplication,并在标准模块“ 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'. 第二个模块名为“ Sporttabs”。
In my application.config.php I've set as found in documentation: 在我的application.config.php文件中,我已按照文档中的内容进行设置:
// 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: 在模块“应用程序”中,我在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: 我尝试在index.phtml中链接它:
<a href="<?php echo $this->url('sporttabs',array('module' => 'sporttabs','controller' => 'sporttab','action' => 'index'))?>">Sporttabs-Projekt</a>
This doesn't work, I only get /sporttab 这行不通,我只能得到/ 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...) 即使我尝试执行www.myurl.de/sporttabs,我也不会进入Sporttabs-Module ...(我正在使用ZendStudio生成ne模块,因此我认为所有文件都位于正确的位置... )
Can you give me a hint how to do this ? 你能给我一个提示怎么做吗?
There is no need to define sporttabs inside your Application config. 无需在“应用程序”配置中定义Sporttab。 I suggest you do this inside your Sporttabs' module.config.php file.
我建议您在Sporttabs的module.config.php文件中执行此操作。
This is an example of my /admin
route, which is a different module named Admin
, which sit next to Application. 这是我
/admin
路由的示例,它是一个名为Admin
的不同模块,位于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. /default
是为了访问子路由。
@Stanimir hits the right hint for my solution: @Stanimir为我的解决方案找到了正确的提示:
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. 在为我的应用程序编辑路由时,我必须对路由数组的顺序进行了无意的更改:“ may_terminate”和“ child_routes”部分移到了较高级别,而不是“ fach”路由的一部分。
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,因为控制器的别名从“ Index”更改为“ 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. 相同的错误使我无法进入第二个模块,routes-array的顺序也不正确。
Now the link-solution Stanimir posted in his answer works fine an I get into my new module... 现在,在他的答案中发布的链接解决方案Stanimir可以正常工作,一旦我进入新模块...
Thanks for your help Stanimir! 感谢您的帮助斯坦尼米尔!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.