0

我尝试遵循该主题的建议:zend framework 2 + routing database

我有一个路线类:

    namespace Application\Router;

use Zend\Mvc\Router\Http\RouteInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\Mvc\Router\RouteMatch;

class Content implements RouteInterface, ServiceLocatorAwareInterface {

protected $defaults = array();
protected $routerPluginManager = null;

public function __construct(array $defaults = array()) {
    $this->defaults = $defaults;
}

public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $routerPluginManager) {
    $this->routerPluginManager = $routerPluginManager;
}

public function getServiceLocator() {
    return $this->routerPluginManager;
}

public static function factory($options = array()) {
    if ($options instanceof \Traversable) {
        $options = ArrayUtils::iteratorToArray($options);
    } elseif (!is_array($options)) {
        throw new InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
    }

    if (!isset($options['defaults'])) {
        $options['defaults'] = array();
    }

    return new static($options['defaults']);
}

public function match(Request $request, $pathOffset = null) {
    if (!method_exists($request, 'getUri')) {
        return null;
    }

    $uri = $request->getUri();
    $fullPath = $uri->getPath();

    $path = substr($fullPath, $pathOffset);
    $alias = trim($path, '/');

    $options = $this->defaults;
    $options = array_merge($options, array(
        'path' => $alias
            ));

    return new RouteMatch($options);
}

public function assemble(array $params = array(), array $options = array()) {
    if (array_key_exists('path', $params)) {
        return '/' . $params['path'];
    }

    return '/';
}

public function getAssembledParams() {
    return array();
}

}

注意 match() 函数返回 Zend\Mvc\Router\RouteMatch 实例的对象 但是在 Zend\Mvc\Router\Http\TreeRouteStack 文件中,它检查对象是否是 RouteMatch 的实例(没有命名空间的前缀)

if (
    ($match = $route->match($request, $baseUrlLength, $options)) instanceof RouteMatch
            && ($pathLength === null || $match->getLength() === $pathLength)
        )

由于命名空间,在我的情况下条件失败。

有什么建议么?

4

1 回答 1

0

好的,我弄清楚了问题所在。而不是返回 Zend\Mvc\Router\RouteMatch 我应该返回 Zend\Mvc\Router\Http\RouteMatch

这解决了我的问题

于 2013-11-13T20:32:23.963 回答