ZF2 - 如何检查 child 的路线是否存在
ZF2 - how to check if a route with child exists
给定这条路线:
'country' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/1country',
'defaults' => array(
'controller' => 'Index',
'action' => 'country',
'country_id' => '5',
),
),
'may_terminate' => true,
'child_routes' => array(
'city' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/1city',
'defaults' => array(
'controller' => 'Index',
'action' => 'city',
'city_id' => '4',
),
),
),
),
),
如果我这样做:
$this->getServiceLocator()->get('Router')->hasRoute('country'); //true
$this->getServiceLocator()->get('Router')->hasRoute('country/city'); //false
虽然两者都应该是正确的..有什么想法吗?
试试这个:
// explode if child..
$route = 'home/default';
$names = explode('/', $route, 2);
// use
/* @var Router $router */
$router = $container->get('Router');
if (!$router->hasRoute($names[0])) {
// ...
}
给定这条路线:
'country' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/1country',
'defaults' => array(
'controller' => 'Index',
'action' => 'country',
'country_id' => '5',
),
),
'may_terminate' => true,
'child_routes' => array(
'city' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/1city',
'defaults' => array(
'controller' => 'Index',
'action' => 'city',
'city_id' => '4',
),
),
),
),
),
如果我这样做:
$this->getServiceLocator()->get('Router')->hasRoute('country'); //true
$this->getServiceLocator()->get('Router')->hasRoute('country/city'); //false
虽然两者都应该是正确的..有什么想法吗?
试试这个:
// explode if child..
$route = 'home/default';
$names = explode('/', $route, 2);
// use
/* @var Router $router */
$router = $container->get('Router');
if (!$router->hasRoute($names[0])) {
// ...
}