【问题标题】:ZF2 routing configurationZF2路由配置
【发布时间】:2012-12-30 14:17:59
【问题描述】:

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

当我访问 mydomain.com 时,此路由将按照配置文件中的指定路由到应用程序模块、索引控制器、索引操作。

但是如果我输入 mydomain.com/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',
        ),
    ),
);

application.config.php

<?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',
        ),
    ),
);

【问题讨论】:

    标签: frameworks url-routing zend-framework2


    【解决方案1】:

    配置的默认路由是接受任何这种格式的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 中搜索的。知道为什么吗?
    • 参考与您的根文件夹 ex. 相关的图像。图片/yourimage.jpg
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多