douqiao2008 2015-09-15 21:55
浏览 281
已采纳

Zend Framework 2在模块之间路由

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:

<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

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 ?

  • 写回答

2条回答 默认 最新

  • drd99007 2015-09-16 05:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等