由於我沒有反饋,我不得不採取一種解決方法。以下是我成功地完成我問:
首先,而是採用:
www.example.com/Search/Attribute1-X-Attribute3-Y/Search/Attribute2-Z
我以前是這樣的:
www.example.com/Search/Attribute1/X/Attribute3/Y/Search/Attribute2/Z
我第一次使用正則表達式來這條路對我module.config:
'search' => array(
'type' => 'Application\Router\SearchesImprovedRegex',
'options' => array(
'regex' => '(/Search((/Attribute1/(?<attr1>[a-zA-Z-]+)){0,1}(/Attribute2/(?<attr2>[a-zA-Z-]+)){0,1}(/Attribute3/(?<attr3>[a-zA-Z-]+)){0,1}))+',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
'spec'=> '/'
),
),
然後我創建了自己的類,擴展的Zend \的mvc \路由器\ HTTP \正則表達式和IM贊成RouterInterface。我只需要重寫匹配函數來使它對屬性有一些不同的解析。
<?php
namespace Application\Router;
use Zend\Mvc\Router\Http\Regex as Regex;
use Zend\Mvc\Router\Http\RouteMatch;
use Zend\Stdlib\RequestInterface as Request;
/**
* Regex route.
*/
class SearchesImprovedRegex extends Regex {
/**
* match(): defined by RouteInterface interface.
*
* @param Request $request
* @param int $pathOffset
* @return RouteMatch|null
*/
public function match(Request $request, $pathOffset = null) {
if (!method_exists($request, 'getUri')) {
return null;
}
$uri = $request->getUri();
$path = $uri->getPath();
$auxPath = explode('/Search', $path);
$searches = Array();
for ($i = 1; $i < count($auxPath); $i++) {
$search = array();
$path = '/Search' . $auxPath[$i];
if ($pathOffset !== null) {
$result = preg_match('(\G' . $this->regex . ')', $path, $matches, null, $pathOffset);
} else {
$result = preg_match('(^' . $this->regex . '$)', $path, $matches);
}
if (!$result) {
return null;
}
$matchedLength = strlen($matches[0]);
foreach ($matches as $key => $value) {
if (is_numeric($key) || is_int($key) || $value === '') {
unset($matches[$key]);
} else {
$search[$key] = rawurldecode($value);
}
}
array_push($searches, $search);
}
$matches = Array('Searches' => $searches);
return new RouteMatch(array_merge($this->defaults, $matches), $matchedLength);
}
}
然後在控制器:
var_dump($this->params('Searches'));