在Zend Framework中遇到模块的URI路由问题

时间:2014-10-29 17:34:27

标签: php zend-framework zend-framework2 zend-studio zend-server

我刚开始使用Zend Framework,而且我不太确定我在URI路由方面做错了什么。

我从我的htdocs文件夹中的Zend Studio中的初始Zend Framework项目开始(我在Windows 7上使用Zend Server)。到目前为止的所有内容似乎都能正常工作(索引页面已经用完了)(

)。

但是当我尝试添加一个模块时,在本例中称为带有一个名为Index的控制器的用户,并按照获得配置的说明进行操作,我不确定我应该在URI中添加什么来获取它路由到它的视图。我已经尝试了几乎所有我能想到的URI组合配置(localhost:80/public/userslocalhost:80/public/users/indexlocalhost:80/users等等。

我没有收到路由错误,只是一个简单的404页面。

我是否需要将公用文件夹设置为root?或者我还需要做些什么来使路由工作?

〜编辑以响应bitWorking

看起来它会自动将其添加到application.config.php中。但这是Users模块的module.config.php

'router' => array(
    'routes' => array(
        'users' => array(
            'type'    => 'Literal',
            'options' => array(
                // Change this to something specific to your module
                'route'    => '/index',
                '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.
                '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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),

现在我确实知道它在哪里指导您自定义路线。我也尝试过这个,但仍然不确定我应该将它们设置为什么。更近了。

1 个答案:

答案 0 :(得分:1)

如果要使用/users在“用户”模块中调用索引控制器,则必须相应地命名路径:

...
'users' => array(
    'type'    => 'Literal',
    'options' => array(
        // Change this to something specific to your module
        'route'    => '/users',
                      ---------
        ...

否则请控制application.config.php。它应该看起来像:

return array(
    'modules' => array(
        'Application',
        'Users',
    ),
    ...

所以Url应该是这样的:

localhost/public/users -> Users/Controller/IndexController/indexAction
localhost/public/users/foo -> Users/Controller/FooController/indexAction
localhost/public/users/foo/bar -> Users/Controller/FooController/barAction