2012-09-11 58 views
4

如何使路由自動適用於ZF1結構中的所有內容?ZF2 Routing in ZF1

模塊/控制器/動作/ par1Name/par1Val/par2Name/par2Val/

我讀到的路由信息​​,但我看到它的方式,我必須手動添加的所有行動,我看到了問題可選參數...

回答

7

您可以設置一個通配符child_route,至少在每個控制器的基礎上,得到ZF1樣的路線:

'products' => array(
       'type' => 'Zend\Mvc\Router\Http\Segment', 
       'options' => array(
        'route' => '/products[/:action]', 
        'defaults' => array(
         'controller' => 'Application\Controller\Products', 
         'action' => 'index' 
        ) 
       ), 
       'may_terminate' => true, 
       'child_routes' => array(
        'wildcard' => array(
         'type' => 'Wildcard' 
        ) 
       ) 
      ) 

然後你就可以使用,例如, url()視圖助手:

$this->url('products/wildcard',array('action'=>'edit','id'=>5,'foo'=>'bar'); 

這將產生像/產品/編輯/ ID/5 /富/酒吧

+0

看看[文檔](https://zf2.readthedocs.org/en/latest/modules/zend.mvc.routing.html#zend-mvc-router-http-wildcard-deprecated)看來這種方法現已被棄用;還有其他解決方案嗎? – AlexP

6

這裏的URL是標準的路由器我使用的一切,我整體遷移ZF1 - > ZF2記住,你仍然需要添加控制器作爲我在列表頂部的調用。另外請記住,我始終將我的實際應用程序保留在列表的底部,以便所有其他模塊在其到達應用程序之前定義其路由。然而,這使得路由工作就像ZF1一樣......我看到很多人問這個,所以我想我會發布!使用下面的設置,我可以添加我的新控制器(支持下面...)然後打開瀏覽器並轉到... /支持,它工作得很好。

return array(
    'controllers' => array(
     'invokables' => array(
      'Application\Controller\Index' => 'Application\Controller\IndexController', 
      'Application\Controller\Support' => 'Application\Controller\SupportController', 
     ), 
    ), 
    'router' => array(
     'routes' => array(
      '*' => array(
       'type' => 'Segment', 
       'options' => array(
        'route' => '/[:controller[/:action]]', 
         /* OR add something like this to include the module path */ 
         // 'route' => '/support/[:controller[/:action]]', 
        'constraints' => array(
         'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        ), 
        'defaults' => array(
         '__NAMESPACE__' => 'Application\Controller', 
         'controller' => 'Index', 
         'action'  => 'index', 
        ), 
       ), 
       'may_terminate' => true, 
       'child_routes' => array(
        'wildcard' => array(
         'type' => 'Wildcard' 
        ) 
       ) 
      ), 
     ), 
    ), 
    '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', 
     ), 
    ), 
    /* Service manager/translator etc stuff go HERE as they change less frequently */ 
); 

我編輯上面的路線,包括一個完整的模塊路徑作爲我錯過了我最初發表評論。