zend-framework-mvc - ZF3 zend-mvc 一个模块中许多 Controller 的通用路由不起作用

标签 zend-framework-mvc zend-framework3 zf3

我在 ZF3 中,使用 zend-mvc-sculpture 并尝试配置一个通用路由,该路由将匹配尽可能多的 URL,因为我希望能够创建新的 Controller (当然包括操作方法),并且让它们立即可用。

文档中描述的常见方法是编写与 Controller 和操作相匹配的路由(与 ZF2 相同)。

这是我的module.config.php

namespace Application;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'router' => [
        'routes' => [
            'home' => [
                'type'    => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'default' => [
                'type' => Segment::class,
                'options' => [
                    'route'    => '/application[/:controller[/:action]]',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                    'constraints' => [
                        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [/* ... */],
    'view_manager' => [/* ... */],
],

它就像 http://localhost/ 的魅力一样。和http://localhost/application调用indexAction() IndexController的功能/module/Application/src/IndexController.php里面的类文件。

但是,当我尝试获取 fooAction() 时,它不起作用。在同一个 Controller (即 IndexController)中运行。它没有正确解析http://localhost/application/foo 。我收到以下错误:

A 404 error occurred

Page not found.

The requested controller could not be mapped to an existing controller class.

Controller:
foo (resolves to invalid controller class or alias: foo)

No Exception available

如果我尝试 http://localhost/bar/foo 也会出现同样的错误获取 fooAction()barController .

你知道这有什么问题吗?任何帮助将不胜感激。非常感谢。

最佳答案

路由 http://localhost/application/foo 不会解析为索引 Controller 中的 fooAction(),因为 /foo URL 中的 > 将匹配 Controller 而不是操作。通过该路由设置,您需要访问 http://localhost/application/index/foo

要使其正常工作,您还需要确保在配置中为 Controller 添加了别名,例如假设你有:

'controllers' => [
    'invokables' => [
        'Application\Controller\Index' => \Application\Controller\IndexController::class
    ]
],

然后为 Controller 添加别名,使其与路由参数匹配:

'controllers' => [
    'invokables' => [
        'Application\Controller\Index' => \Application\Controller\IndexController::class
    ],
    'aliases' => [
        'index' => 'Application\Controller\Index'
    ]
],

您需要为每个未使用您想要的路由字符串注册的 Controller 添加与路由参数匹配的别名,例如 Controller Namespace\Controller\BarController 应该别名为 bar 等。

关于zend-framework-mvc - ZF3 zend-mvc 一个模块中许多 Controller 的通用路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38490866/

相关文章:

php - 如何编写正文中包含 anchor 标记的 Zend Framework URL?

php - ZF3 中的服务管理器

php - Zend Framework 中的嵌套 Controller

view-helpers - 如何注册查看助手

php - 如何摆脱 Zend Framework 3 "getting started"教程中的 fatal error ?

php - 互连服务的 ZF3 服务 DI 的有效模式

php - 如何访问 Zend_Framework 中默认模块中的模型?

zend-framework - 使用 OAuth2 和 ZF3-MVC 保护 REST API

php - 实体公共(public)字段的学说继承

php - 使用 Zend Framework 3 构建 RESTful 服务的最佳方式