繁体   English   中英

ZF2中Zend \\ Mvc \\ Router \\ RouteInterface :: assemble()的输出应该是什么

[英]What schould be the output of Zend\Mvc\Router\RouteInterface::assemble() in ZF2

我正在为Zend Framework 2项目编写我的自定义路由器,该项目扩展了Zend \\ Mvc \\ Router \\ RouteInterface 路由应来自数据库(具有数百页的大型项目)。 工作的路由器显然只需要两种方法: match()assemble() 我开始工作的火柴。

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

这是ZF2的内部路由器( Zend \\ Mvc \\ Router \\ SimpleRouteStack )之一:

/**
 * 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);
}

参考: Zend Framework 2中的自定义路由

当您执行$this->redirect-toRoute($name, $params);类的操作时,基本上就是汇编$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',
        ),
    ),
),

当我们调用$this->redirect-toRoute('test', array('id' => 1));此路由名为'test' $this->redirect-toRoute('test', array('id' => 1)); 路由栈将找到用于“ test”的实例化路由,这是一个\\ Zend \\ Mvc \\ Router \\ Http \\ Segment,然后调用它的汇编函数,该函数将在对toRoute的调用中使用发送的参数,并将产生一个URL像这样的字符串

/测试/ 1

这基本上就是汇编功能的工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM