ZF2路由 - 如何配置这些路由

时间:2015-09-08 12:15:43

标签: php routing zend-framework2

我有以下ZF2配置:

<?php

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                            '__NAMESPACE__' => 'Application\Controller',
                        'controller' => 'Index',
                        'action'     => 'index',
                    ),
                ),
            ),
            'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '[:controller[/:action]][/:id][/:pId][/:devId]', //edited by Dibyendu
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'abstract_factories' => array(
            'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
            'Zend\Log\LoggerAbstractServiceFactory',
        ),
        'factories' => array(
            'translator' => 'Zend\Mvc\Service\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',
                'Application\Controller\Upload' => 'Application\Controller\UploadController'
        ),
    ),
    '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',
        ),
    ),
    // Placeholder for console routes
    'console' => array(
        'router' => array(
            'routes' => array(

            ),
        ),
    ),
);

现在我有控制器Upload,它有三个动作:

  1. indexAction
  2. uploadtestAction
  3. uploadmusicAction
  4. 一般来说,这些网页的网址如下:

    1. http://localhost/zend_test/public/Upload
    2. http://localhost/zend_test/public/Upload/uploadtest
    3. http://localhost/zend_test/public/Upload/uploadmusic
    4. 分别因此我想让它们像:

      1. http://localhost/zend_test/public/Upload/upload-home
      2. http://localhost/zend_test/public/Upload/upload-test
      3. http://localhost/zend_test/public/Upload/musics-upload
      4. 最后两个在URL中有四个参数。 我该如何实现呢?

1 个答案:

答案 0 :(得分:0)

我的工作空间没有打开,所以这是在黑暗中拍摄的。 抱歉

您是否尝试过使用类似于RESTful服务的架构? This file是我经常使用的服务控制器,它给了我类似于你的路由。

  • 纯粹意义上,您的控制器将采取一项措施。
  • 您的路线将细分映射到参数。
  • indexAction使用该参数,并将方法链传递给您选择的内部函数。

不要通过REST标准判断我的服务。它让我觉得那里的服务很脏:) 来更新它一天。

在您的用例中:

您的路线看起来像

'upload' => array(
    'type' => 'Literal',
    'options' => array(
      // Change this to something specific to your module
      'route' => '/Upload',
      'defaults' => array(
        '__NAMESPACE__' => 'Application\Controller',
        'controller' => 'Upload',
      ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
      'client' => array(
        'type' => 'Segment',
        'options' => array(
          'route' => '[/:type][/:id]',
          'constraints' => array(
            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
          ),
          'defaults' => array(
            'controller' => 'Upload',
            'action' => 'index'
          ),
        ),
      ),
    ),
  ),

并且您的indexController将在其切换中使用$type来指导流程。