zend框架2路由配置和参数传递

时间:2014-02-18 15:36:03

标签: zend-framework2

我已经启动了一个zend2项目,我无法弄清楚路由系统。我真的需要一些帮助将一些变量从视图传递回控制器。模块配置如下所示:

<?php
return array(
'controllers' => array(
    'invokables' => array(
        'Map\Controller\Index' => 'Map\Controller\IndexController',
    ),
),
'router' => array(
    'routes' => array(
        'map' => array(
            'type'    => 'segment',
            'options' => array(
                // Change this to something specific to your module
                'route'    => '/map[/:action][/:tileId]',
                'defaults' => array(
                    // Change this value to reflect the namespace in which
                    // the controllers for your module are found
                    '__NAMESPACE__' => 'Map\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                // This route is a sane default when developing a module;
                // as you solidify the routes for your module, however,
                // you may want to remove it and replace it with more
                // specific routes.
                '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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'map' => __DIR__ . '/../view',
    ),
),
);

我的控制器中有一个方法2方法,我想传递参数:

public function buyTileAction($tileId){
    echo $tileId; 
}

和类似的问题是,我尝试“myappname.dev/map/buy-tile”并输入方法并打印一个字符串,例如,但是当我尝试传递像“myappname”这样的参数时。 dev / map / buy-tile / tileId / 1“我没有收到控制器中的值。

提前谢谢

2 个答案:

答案 0 :(得分:2)

这应该做你需要做的事。

public function buyTileAction() {
    $tileId = $this->params()->fromRoute('tileId');
}

答案 1 :(得分:0)

尝试:

$tileId = $this->getEvent()->getRouteMatch()->getParam('tileId');