2015-02-08 133 views
0

我正在为一个扩展为Zend\Mvc\Router\RouteInterface的Zend Framework 2项目编写我的自定义路由器。路线应该来自数据库(大型项目有数百页)。工作路由器显然只需要两种方法:match()assemble()。我工作的比赛一切正常。ZF2中Zend Mvc Router RouteInterface :: assemble()的输出是什么

但是assemble()呢? 该方法返回什么?难道它只是返回应用程序的基本路径?

这里是内部路由器的什么人(Zend\Mvc\Router\SimpleRouteStack)ZF2的作用:

/** 
* assemble(): defined by RouteInterface interface. 
* 
* @see \Zend\Mvc\Router\RouteInterface::assemble() 
* @param array $params 
* @param array $options 
* @return mixed 
* @throws Exception\InvalidArgumentException 
* @throws Exception\RuntimeException 
*/ 
public function assemble(array $params = array(), array $options = array()) 
{ 
    if (!isset($options['name'])) { 
     throw new Exception\InvalidArgumentException('Missing "name" option'); 
    } 

    $route = $this->routes->get($options['name']); 

    if (!$route) { 
     throw new Exception\RuntimeException(sprintf('Route with name "%s" not found', $options['name'])); 
    } 

    unset($options['name']); 

    return $route->assemble(array_merge($this->defaultParams, $params), $options); 
} 

参考:Custom Routing in Zend Framework 2

回答

1

基本上组装时,当你做这样的事情$this->redirect-toRoute($name, $params);

会有什么叫所以它应该根据路由配置返回一个URL字符串。该路由可以使用相同的路由配置进行匹配。

当你调用toRoute,您张贴找到与您在调用中指定的名称的路线,然后要求它的URL装配到该路由

'test' => array(
    'type' => 'Segment', 
    'options' => array(
     'route' => '/test[/:id]', 
     'constraints' => array(
      'id'  => '[a-zA-Z][a-zA-Z0-9_-]*', 
     ), 
     'defaults' => array(
      '__NAMESPACE__' => 'Application\Controller', 
     ), 
    ), 
), 

这条路线命名为“测试”时,我们的routestack调用$this->redirect-toRoute('test', array('id' => 1));路由堆栈将找到'test'的实例化路由,这是一个\ Zend \ Mvc \ Router \ Http \ Segment,然后调用它的组装函数,它将调用toRoute时的参数发送给它,一个这样的URL字符串

/test/1

这基本上就是汇编函数的作用。