如何在zend2中向同一个控制器添加多个动作

时间:2012-10-30 05:22:05

标签: php zend-framework2

我需要为现有的控制器添加'addAction'和' deleteAction '。我使用了zendframework-ZendSkeletonApplication-zf-release-2.0.3,我在IndexController中添加了这两个动作,还有我为index文件夹中的每个操作创建了2个页面。但它如何添加到module.config.php这是代码我的module.config.php文件

<?php

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/application',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),
            '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',
        ),
    ),
);

请帮帮我。

1 个答案:

答案 0 :(得分:2)

这是我在项目中使用的示例路由配置,其中所有内容都基于根路径/

'router' => array(
    'routes' => array(
        'kz' => array(
            'type'    => 'literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Kennzahlen\Controller\Index',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'add' => array(
                    'type' => 'literal',
                    'options' => array(
                        'route' => 'add',
                        'defaults' => array(
                            'action' => 'add'
                        )
                    )
                ),
                'edit' => array(
                    'type' => 'literal',
                    'options' => array(
                        'route' => 'edit',
                        'defaults' => array(
                            'action' => 'edit'
                        )
                    )
                )
            )
        ),
    ),
),

名为kz的第一条路线有basePath /

之后,您有两个名为addedit的子路由,它们会添加或编辑基本路径,因此它们的完整路径变为/add和{ {1}}

请考虑查看the online Documentation以进一步澄清有关路由的问题,因为这确实是一个很好的起点。

或者您也可以查看ZendCon的DASPRiDs Router Presentation