2

我在 ZF3 中,使用 zend-mvc-skeleton 并尝试配置一个通用路由,该路由将匹配尽可能多的 URL,以便能够创建新的控制器(当然包括操作方法),并立即拥有它们可用的。

文档中描述的常用方法是编写与控制器和操作匹配的路由(与 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()在同一个控制器(即 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/foofooAction().barController

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

4

2 回答 2

2

路由http://localhost/application/foo不会fooAction()在索引控制器中解析,因为/foo在 URL 中将匹配控制器而不是操作。使用该路线设置,您需要访问http://localhost/application/index/foo.

为了让它工作,你还需要确保你在配置中为你的控制器起别名,例如假设你有:

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

然后为控制器设置别名,使其与路由参数匹配:

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

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

于 2016-07-22T09:28:03.937 回答
0

我带着类似的问题来到这里。我在“应用程序”模块中创建了两个控制器,并在新模块“帐户”中创建了两个具有相同名称的控制器。

Application/Controller/IndexController
Application/Controller/OverviewController
Account/Controller/IndexController
Account/Controller/OverviewController

这是我的 modules.config.php

模块/帐户/config/module.config.php

 return [
'router' => [
    'routes' => [
        'Account-account' => [
         'type'  =>  Segment::class,
         'options' =>  [
          'route' =>  '/account[/][:controller[/][:action][/]]',
           'defaults'  =>  [
             '__NAMESPACE__' =>  'Account\Controller',
             'controller'  =>  Account\Controller\IndexController::class,
             'action'      =>  'index',
             'locale'  =>  'en_us'
           ],
         ],
         'may_terminate' =>  true,
         'child_routes'  => [
           'wildcard'  =>  [
             'type'  =>  'Wildcard'
           ],
         ],
       ],
    ],
],
'controllers' => [
    'factories' => [
        Controller\IndexController::class     => AccountControllerFactory::class,
        Controller\OverviewController::class  => AccountControllerFactory::class,
    ],
    'aliases' => [
      'index'   =>  IndexController::class,
      'overview'  =>  OverviewController::class
    ]

],

和我的模块/Application/config/module.config.php

return [
    'router' => [
        'routes' => [
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'Application-application' => [
             'type'  =>  Segment::class,
             'options' =>  [
              'route' =>  '/application[/][:controller[/][:action][/]]',
               'defaults'  =>  [
                 '__NAMESPACE__' =>  'Application\Controller',
                 'controller'  =>  Application\Controller\IndexController::class,
                 'action'      =>  'index',
                 'locale'  =>  'en_US'
               ],
             ],
             'may_terminate' =>  true,
             'child_routes'  => [
               'wildcard'  =>  [
                 'type'  =>  'Wildcard'
               ],
             ],
           ],
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => IndexControllerFactory::class,
            Controller\OverviewController::class => IndexControllerFactory::class,
        ],
        'aliases' =>  [
          'index'     =>  IndexController::class,
         'overview'  =>  OverviewController::class,
        ]

    ],

使用此配置,如果别名部分被注释,则会出现一条错误消息,指出存在无效的控制器或别名(索引/概述)。如果有别名路由:“应用程序/概述/索引”进入帐户模块。

于 2016-12-08T08:08:52.937 回答