/**
  * Inject route into the underlying router implemetation.
  *
  * @param Route $route
  */
 private function injectRoute(Route $route)
 {
     $name = $route->getName();
     $path = $route->getPath();
     $options = $route->getOptions();
     $options = array_replace_recursive($options, ['route' => $path, 'defaults' => ['middleware' => $route->getMiddleware()]]);
     $allowedMethods = $route->getAllowedMethods();
     if (Route::HTTP_METHOD_ANY === $allowedMethods) {
         $this->zendRouter->addRoute($name, ['type' => 'segment', 'options' => $options]);
         $this->routeNameMap[$name] = $name;
         return;
     }
     // Remove the middleware from the segment route in favor of method route
     unset($options['defaults']['middleware']);
     if (empty($options['defaults'])) {
         unset($options['defaults']);
     }
     $httpMethodRouteName = implode(':', $allowedMethods);
     $httpMethodRoute = $this->createHttpMethodRoute($route);
     $methodNotAllowedRoute = $this->createMethodNotAllowedRoute($path);
     $spec = ['type' => 'segment', 'options' => $options, 'may_terminate' => false, 'child_routes' => [$httpMethodRouteName => $httpMethodRoute, self::METHOD_NOT_ALLOWED_ROUTE => $methodNotAllowedRoute]];
     if (array_key_exists($path, $this->allowedMethodsByPath)) {
         $allowedMethods = array_merge($this->allowedMethodsByPath[$path], $allowedMethods);
         // Remove the method not allowed route as it is already present for the path
         unset($spec['child_routes'][self::METHOD_NOT_ALLOWED_ROUTE]);
     }
     $this->zendRouter->addRoute($name, $spec);
     $this->allowedMethodsByPath[$path] = $allowedMethods;
     $this->routeNameMap[$name] = sprintf('%s/%s', $name, $httpMethodRouteName);
 }
 public function setUp()
 {
     $router = new TreeRouteStack();
     $router->addRoute('home', LiteralRoute::factory(['route' => '/', 'defaults' => ['controller' => TestAsset\SampleController::class]]));
     $router->addRoute('sub', SegmentRoute::factory(['route' => '/foo/:param', 'defaults' => ['param' => 1]]));
     $router->addRoute('ctl', SegmentRoute::factory(['route' => '/ctl/:controller', 'defaults' => ['__NAMESPACE__' => 'ZendTest\\Mvc\\Plugin\\Prg\\TestAsset', 'controller' => 'sample']]));
     $this->controller = new TestAsset\SampleController();
     $this->request = new Request();
     $this->event = new MvcEvent();
     $this->routeMatch = new RouteMatch(['controller' => 'controller-sample', 'action' => 'postPage']);
     $this->event->setRequest($this->request);
     $this->event->setRouteMatch($this->routeMatch);
     $this->event->setRouter($router);
     $this->controller->setEvent($this->event);
     $this->plugin = new PostRedirectGet();
     $this->plugin->setController($this->controller);
 }
 /**
  * assemble(): defined by \Zend\Router\RouteInterface interface.
  *
  * @see    \Zend\Router\RouteInterface::assemble()
  * @param  array $params
  * @param  array $options
  * @return mixed
  * @throws Exception\InvalidArgumentException
  * @throws Exception\RuntimeException
  */
 public function assemble(array $params = [], array $options = [])
 {
     if ($this->hasTranslator() && $this->isTranslatorEnabled() && !isset($options['translator'])) {
         $options['translator'] = $this->getTranslator();
     }
     if (!isset($options['text_domain'])) {
         $options['text_domain'] = $this->getTranslatorTextDomain();
     }
     return parent::assemble($params, $options);
 }
示例#4
0
文件: Part.php 项目: noose/zf2
 /**
  * assemble(): Defined by Route interface.
  *
  * @see    Route::assemble()
  * @param  array $params
  * @param  array $options
  * @return mixed
  */
 public function assemble(array $params = null, array $options = null)
 {
    if ($this->childRoutes !== null) {
         $this->addRoutes($this->childRoutes);
         $this->childRoutes = null;
     }
     
     $uri = $this->route->assemble($params, $options)
          . parent::assemble($params, $options);
     
     return $uri;
 }
示例#5
0
 /**
  * assemble(): Defined by RouteInterface interface.
  *
  * @see    \Zend\Router\RouteInterface::assemble()
  * @param  array $params
  * @param  array $options
  * @return mixed
  * @throws Exception\RuntimeException
  */
 public function assemble(array $params = [], array $options = [])
 {
     if ($this->childRoutes !== null) {
         $this->addRoutes($this->childRoutes);
         $this->childRoutes = null;
     }
     $options['has_child'] = isset($options['name']);
     if (isset($options['translator']) && !isset($options['locale']) && isset($params['locale'])) {
         $options['locale'] = $params['locale'];
     }
     $path = $this->route->assemble($params, $options);
     $params = array_diff_key($params, array_flip($this->route->getAssembledParams()));
     if (!isset($options['name'])) {
         if (!$this->mayTerminate) {
             throw new Exception\RuntimeException('Part route may not terminate');
         } else {
             return $path;
         }
     }
     unset($options['has_child']);
     $options['only_return_path'] = true;
     $path .= parent::assemble($params, $options);
     return $path;
 }