2014-02-18 48 views
0

我已經開始了一個zend2項目,我找不到路由系統。我真的需要一些幫助,將一些變量從視圖傳遞迴控制器。該模塊的配置看起來它如下:zend框架2路由配置和參數傳遞

<?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「這樣的參數時,我不' t接收控制器中的值。

謝謝你提前

回答

0

嘗試:

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

您好,嘗試這兩種解決方案(@Adam H公司和@ KeyneON的),沒有給我預期的結果。而不是打印變量的值,該請求會提供「請求的URL無法通過路由進行匹配」。當我嘗試訪問myappname.dev/map/buy-tile/tileId/1時,任何人有任何其他想法如何解決這個問題?謝謝。 – Susy11

2

這應該做你需要做的。

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