2012-09-11 61 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 */ 
); 

我编辑上面的路线,包括一个完整的模块路径作为我错过了我最初发表评论。