Zend Framework 2 - URL多模块

4

我目前正在使用Git上的ZendFrameworkSkeleton应用程序,并尝试利用其中的模块部分来拥有多个模块,可以通过URL进行更改,如下所示:

http://localhost/application/index/index/
http://localhost/guestbook/index/index/
http://localhost/forum/index/index/

此外,未来扩展还需要如何使用语言:
http://localhost/en/application/index/index/
http://localhost/de/application/index/index/
http://localhost/en/forum/index/index/

我本以为这就是ZF2模块的全部意义,但很惊讶它似乎并没有开箱即用。有人知道应该如何做或者有示例/教程的链接吗?
目前看来,路由器在每个模块中而不是整个应用程序中,我认为应该是整个应用程序处理路由。
我猜你需要一个应用程序模块来处理路由和全局内容,注入依赖项等等,然后其他模块用于不同的事物,例如游戏、帐户、留言板、论坛等等。
一旦我弄清楚了,我可以制作一个Github示例应用程序,因为我知道其他人也很好奇。
编辑@24/11/2011:我后来在贡献者论坛上看到EvanDotPro的一篇文章,他们谈论不想使用ZF1模块/控制器/操作方式,并且对此需求不是太大。他实际上写了一个类似于这样运行的示例,但说它不能百分之百地工作。所以任何遇到这篇文章并正在寻找更多信息并且比较精通的人,这是它:https://github.com/EvanDotPro/EdpMagicRoute(如果在阅读时仍然存在!)

2
听起来像是通过自定义路由实现的东西。 - Phil
我以为只需要添加类似于:/%module%/%controller%/%action%/ 就可以了,但似乎并不起作用。 - Intellix
嗯,我搜索了一下,没有找到任何东西。我猜可以把这两个合并起来...但我不太确定该怎么做。 - Intellix
在使用了ZF1中的模块/控制器/操作一段时间,并在ZF2中相当多地使用了控制器/操作后,我认为具有模块/控制器/操作是过度设计,强制您拥有不必要的长URL。 - Intellix
3个回答

4

要更改路由,您需要编辑Application/confid/module.config.php。在那里找到并更改为

'options' => array(
    'route' => '/[:module/[:controller[/:action]]]', 
    'constraints' => array(
        'module' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
    ), 
    'defaults' => array(
        'module' => 'Application', 
        'controller' => 'index', 
        'action' => 'index'
    )
)

您可以看到,我添加了 /[:module 和 deafults 和 constraint。

2
能否将路由更改为子域名? - ViniciusPires

4

感谢 zf2-tutorial,我已经搜索了很多例子,但只有在这里找到了表单的用法。 - rdo

1

您可以在位于 module\Application\config 文件夹下的 module.config.php 文件中使用 'child_routes' 属性。

'routes' => array(
        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/application',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    '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(
                        ),
                    ),
                ),
            ),
        ),
    ),

然后你可以运行 localhost/application/index/index。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接