Zend\Navigation\Page\Mvc - IsActive Function "improvement ?" #53
Description
Hello
Right now to check if a Mvc Page is active you guys check if
$this->routeMatch->getMatchedRouteName() === $this->getRoute()
and that the parameters match
(count(array_intersect_assoc($reqParams, $pageParams)) == count($pageParams)
I was wondering if we could improve this behavior by checking first if the Page route is actually contained in the Matched Route
Lets take an example:
Router conf:
'routes' => array(
'production' => [
'type' => 'segment',
'may_terminate' => true,
'options' => [
'route' => '/production',
'defaults' => array(
'__NAMESPACE__' => 'Production\Controller',
'controller' => 'Index',
'action' => 'index'
)
],
'child_routes' => [
'project' => [
'type' => 'segment',
'options' => [
'route' => '/project[/:action[/:id]]',
'defaults' => array(
'controller' => 'Project',
'action' => 'index'
)
],
],
This assume that the child route project is under production
so route "production/project" is a child of "production"
Navigation Conf:
'navigation' => [
'production' => [
'id' => 'project',
'label' => 'Production label',
'route' => 'production'
The current isActive function does not work in this situation because the routes are different
The workaround it to create a child navigation under "production" but i feel like it is overkill (as i dont want to display the child page in the navigation (so i use the param visible = 0 ))
My solution would be to check that the page route is contained in the Matched route
strpos($this->routeMatch->getMatchedRouteName(), $this->getRoute().'/') === 0
In my scenario It would check if "production/" is contained in the "production/project" (at the beginning of the string)
And make the Page active
so if you are viewing the site via the "production/project" route it will make all navigation set up to use the route "production" active
Please let me know if that make sense or if I am missing something that would make this solution incorrect
Thank you