简体   繁体   中英

ZF2 routing - How to configure these routes

I have the following ZF2 configuration:

<?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(

            ),
        ),
    ),
);

Now I have controller Upload which has three actions:

  1. indexAction
  2. uploadtestAction
  3. uploadmusicAction

In general the url of these pages will be like:

  1. http://localhost/zend_test/public/Upload
  2. http://localhost/zend_test/public/Upload/uploadtest
  3. http://localhost/zend_test/public/Upload/uploadmusic

Respectively, hence I want to make them like:

  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

The last two will have four parameter in the URL. How can I implement this?

I don't have my workspace open, so this is a shot in the dark. sorry

Have you tried using an architecture like you'd use for a RESTful service? This file is a service controller I use quite frequently, and it gives me routes similar to yours.

  • Your controller will 1 action, in the pure sense.
  • Your route maps a segment to a param.
  • The indexAction consumes that param, and passes the method chain on to an internal function of your choosing.

please don't judge my service by REST standards. it makes me feel dirty having that service out there :) have to update it one day.

In your use-case:

Your route would look something like

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

and your indexController will consume $type in its switch, to direct the flow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM