我有一个使用zf2的网络应用程序。当它启动时,它显示如下URL:
我需要让它们像:
我尝试使用module.config.php,但没有用:
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Auth\Controller\Index',
'action' => 'login',
),
),
),
'auth' => array(
'type' => 'Literal',
'options' => array(
'route' => '/auth',
'defaults' => array(
'__NAMESPACE__' => 'Auth\Controller',
'controller' => 'Auth\Controller\Index',
'action' => 'login',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
任何帮助将不胜感激!
答案 0 :(得分:0)
解决方案一:
'neighborhoods' => array(
'type' => 'Literal',
'options' => array(
'route' => '/neighborhoods',
'defaults' => array(
'module' => 'auth',
'controller' => 'neighborhoods',
'action' => 'neighborhoods',
),
),
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:id]',
'constraints' => array(
'id' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
'comments' => array(
'type' => 'Literal',
'options' => array(
'route' => '/comments',
'defaults' => array(
'module' => 'auth',
'controller' => 'comments',
'action' => 'comments',
),
),
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:id]',
'constraints' => array(
'id' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
'themes' => array(
'type' => 'Literal',
'options' => array(
'route' => '/themes',
'defaults' => array(
'module' => 'auth',
'controller' => 'themes',
'action' => 'themes',
),
),
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:id]',
'constraints' => array(
'id' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
解决方案2:自定义路线
http://www.zendexperts.com/2012/12/09/custom-routing-in-zend-framework-2/
in module.config.php
'routeTestCustom' => array(
'type' => 'pageRoute',
),
在Module.php中
use [modulename]\Route\PageRoute;
....
public function getRouteConfig()
{
return array(
'factories' => array(
'pageRoute' => function ($routePluginManager) {
$locator = $routePluginManager->getServiceLocator();
$params = array('defaults' => array('module' => 'auth' ));
$route = PageRoute::factory($params);
$route->setServiceManager($locator);
return $route;
},
),
);
}
[module_name] \ src [module_name] \ Route \ PageRoute.php
中的自定义路由类namespace [module_name]\Route;
use Zend\Mvc\Router\Http\RouteInterface;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Mvc\Router\Http\RouteMatch;
class PageRoute implements RouteInterface
{
protected $defaults;
public function __construct(array $defaults = array())
{
$this->defaults = $defaults;
}
public static function factory($options = array())
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
} elseif (!is_array($options)) {
throw new Exception\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)
{
$uri = $request->getUri();
$path = $uri->getPath();
$part = explode('/',$uri);
if(count($part) == 4 && $part[0] == 'http:' && $part[2] == "www.example.com"){
$params = $this->defaults;
$params['controller'] = $part[3];
$params['action']= $part[3];
}
return new RouteMatch($params);
}
public function assemble(array $params = array(), array $options = array()){
return $this->route;
}
public function getAssembledParams()
{
return array();
}
}
<强>更新强>