Frameworks ZF2路由配置

Frameworks ZF2路由配置,frameworks,url-routing,zend-framework2,Frameworks,Url Routing,Zend Framework2,我刚刚构建了一个zf2项目,我遇到了一个配置问题 当我转到mydomain.com时,它会按照配置文件中的指定路由到应用程序模块、索引控制器和索引操作 但如果我键入mydomain.com/otheraction,则不会将其路由到应用程序模块、索引控制器和otheraction。当然,因为它没有配置成这样 所以我的问题是如何配置应用程序来实现这一点?我已经添加了Application/config/module.config.php文件和主配置文件。谢谢大家! module.config.php

我刚刚构建了一个zf2项目,我遇到了一个配置问题

当我转到mydomain.com时,它会按照配置文件中的指定路由到应用程序模块、索引控制器和索引操作

但如果我键入mydomain.com/otheraction,则不会将其路由到应用程序模块、索引控制器和otheraction。当然,因为它没有配置成这样

所以我的问题是如何配置应用程序来实现这一点?我已经添加了Application/config/module.config.php文件和主配置文件。谢谢大家!

module.config.php

    <?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),
            // The following is a route to simplify getting started creating
            // new controllers and actions without needing to create a new
            // module. Simply drop new controllers in, and you can access them
            // using the path /application/:controller/:action
            'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/application',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    '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(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/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(
            __DIR__ . '/../view',
        ),
    ),
);
<?php
return array(
    'modules' => array(
        'Application',
        'ZfcBase',
        'ZfcUser',
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);

在routes子数组中,添加一个类似于“home”条目的下一个条目,例如:

'user' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal',
    'options' => array(
        'route'    => '/user',
        'defaults' => array(
            'controller' => 'Application\Controller\User',
            'action'     => 'some-action',
        ),
    ),
),

这会将您的page.com/user路由到
UserController中的
someActionAction

默认路由配置为接受此格式的任何url,这是标准格式

mydomain.com/
mydomain.com/applicatiom/controller/action (standard format)
在您的示例mydomain.com/otheraction中,路由器期望有一个模块otheraction,但它不存在

如果您需要如上所述的路线,请在“路线”下添加一个条目

'otheraction' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal',
    'options' => array(
        'route'    => '/otheraction',
        'defaults' => array(
            'controller' => 'Application\Controller\Index',
            'action'     => 'otheraction',
        ),
    ),
),

谢谢@Raj!我尝试过,但当我访问mydomain.com/application/index/someaction时,页面会加载,但我的图像会在mydomain.com/application/index/images中搜索,而不是在mydomain.com/images中搜索。知道为什么吗?请参考与根文件夹相关的图像,例如images/yourmage.jpg