【问题标题】:Having trouble with URI routing for modules in Zend FrameworkZend Framework 中模块的 URI 路由有问题
【发布时间】:2014-10-29 17:34:26
【问题描述】:

我刚开始使用 Zend 框架,我不太确定我在 URI 路由上做错了什么。

我从 Zend Studio 中的一个初始 Zend Framework 项目开始,该项目位于我的 htdocs 文件夹中(我也在 Windows 7 上使用 Zend Server)。到那里的一切似乎都可以正常运行索引页面(它已经用完了 /public/ 子目录)。

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

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

我需要将公用文件夹设置为根目录吗?或者我还需要做些什么才能让路由正常工作?

~响应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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),

现在我确实看到了它引导您自定义路线的位置。我也尝试过这个,但仍然不确定我应该将它们设置为什么。不过更近了。

【问题讨论】:

  • 您已将该模块添加到application.config.php?否则请从module.config.php..发布路线。

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


【解决方案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

【讨论】:

  • 我最终得到了这个工作,但只有在切换到 XAMPP 来运行我的 localhost 而不是 Zend Server 之后。使用 Zend 服务器,我不断收到 404 页面,就像它甚至没有尝试路由 URI 一样。我不确定我的配置是否有问题。
猜你喜欢
  • 2016-04-15
  • 1970-01-01
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多