Zend 框架 2 模块名称路由问题 [英] Zend framework 2 module name routing issue

查看:26
本文介绍了Zend 框架 2 模块名称路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有模块路由问题.我有 2 个模块,应用程序和管理.每个模块都有 indexAction 作为默认操作:

I have a problem with module routing. I have 2 modules, Application and Admin. Each modules have indexAction as default action:

localhost/--> 应用程序/索引

localhost/ --> Application/index

本地主机/管理员/-> 管理员/索引

localhost/admin/ -> Admin/index

Admin/index 仅适用于 localhost/admin/index/

Admin/index works only with localhost/admin/index/

当模块名称以字母A"开头时会发生此问题.如果我将 Admin 重命名为Cars",则 localhost/cars/工作正常!

This problem happens when a module name starts with the letter "A". If I rename Admin to "Cars", localhost/cars/ works correctly!

错误是:

A 404 error occurred
The requested controller was unable to dispatch the request.
Controller:
Application\Controller\Application
No Exception available

这是应用程序模块中的module.config.php:

This is module.config.php inside Application module:

<?php
return array(
    'router' => array(
         'routes' => array(
             'Application' => array(
                 'type'    => 'Segment',
                 'options' => array(
                     'route'    => '[/][:action/]',
                     'constraints' => array(
                         'action' => '[a-zA-Z][a-zA-Z0-9_-]*',                         
                     ),
                     'defaults' => array(
                         'controller' => 'Application\Controller\Application',
                         'action'     => 'index',
                     ),
                 ),
             ),
         ),
     ),
   'controllers' => array(
        'invokables' => array(
            'Application\Controller\Application' => 'Application\Controller\IndexController'
        ),
    ),

     'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'Application/Application/index' => __DIR__ . '/../view/Application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),  
        'template_path_stack' => array(
            'Application' => __DIR__ . '/../view',
        ),
    ),
);
?>

这是管理模块中的module.config.php:

this is module.config.php inside Admin module:

    <?php
return array(
    'router' => array(
         'routes' => array(
             'Admin' => array(
                 'type'    => 'Segment',
                 'options' => array(
                     'route'    => '/admin/[:action/]',
                     'constraints' => array(
                         'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
                     ),
                     'defaults' => array(
                         'controller' => 'Admin\Controller\AdminController',
                         'action' => 'index'
                     ),
                 ),
             ),
         ),
     ),
    'controllers' => array(
        'invokables' => array(
            'Admin\Controller\AdminController' => 'Admin\Controller\AdminController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'template_path_stack'      => array(
            'Admin' => __DIR__ . '/../view',
        ),
    ), 
);
?>

IndexController.php

IndexController.php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{

    public function indexAction(){

    }
}

AdminController.php

AdminController.php

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class AdminController extends AbstractActionController
{

    public function indexAction()
    {}
}

有人可以帮我吗?

推荐答案

首先你的错误请求的控制器无法调度请求.仅在路由器无法将请求调度到其定义的动作.因此,请验证您的控制器是否正确,操作是否存在且可调用.

First your error The requested controller was unable to dispatch the request. only occures when the router can't dispatch the request to its defined action. So please verify that your controller are correct and the actions are present and callable.

正如已经指出的,您的 /admin/ 路由将指向两个配置的端点.当在配置中的应用程序路由之前定义管理路由时,这首先不是问题.

As allready pointed out your /admin/ route will point to two configured endpoints. This is not a problem in the first place when the admin route would be defined before the application route in the config.

因此,您的路由 /admin/ 永远不会路由到您的 AdminController,因为其他动态路由将首先匹配.

So your route /admin/ route would never be routed to your AdminController as the other dynamic route would be matched first.

为了获得预期的结果,请在您的路线中使用 priority 设置,以确保您的 Admin 路线将在您的 Application 路线之前匹配.

To get you expected result use the priority setting in your route, to make sure your Admin route will be matched before your Application route.

'router' => array(
     'routes' => array(
         'Admin' => array(
             'priority' => 100,
             'type'    => 'Segment',
             'options' => array(
                 'route'    => '/admin/[:action/]',
                 'constraints' => array(
                     'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
                 ),
                 'defaults' => array(
                     'controller' => 'Admin\Controller\AdminController',
                     'action' => 'index'
                 ),
             ),
         ),
     ),
 ),

<小时>

除了您的问题之外,不要以 ?> 结束 php 脚本,因为当结束标记之后有空格时,这可能会导致错误.


Beside your question, don't end php scripts with ?> as this could lead to bugs when a whitespace is after the end tag.

这篇关于Zend 框架 2 模块名称路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆