I have a CMS where the admin builds its own recursive page structure. Every page has its own "url-key". Next the url-key is used in combination with the page structure to build the URL structure so you get the following structure:
{rootpage url-key}/{subpage url-key}
Optionally, multilanguage supporst is on and you also get a language prefix:
{language code}/{rootpage url-key}/{subpage url-key}
I almost got the above working by providing each page with its own static route and then chaining them together.
/**
* Initializes the sites navigation.
*
* @param array $pages An array with the cms pages to initialize
*/
protected function _initNavigation(array $pages = null)
{
$root = true;
if ($pages === null) {
$pages = Sjr_App::get()->getModel('page')->getTree();
$root = true;
}
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
foreach ($pages as $page) {
$router->addRoute($page->getRouteName(), $page->getRoute());
if ($page->children) {
$this->_initNavigation($page->children);
}
}
}
$page->getRoute():
/**
* Gets the routing for the page.
*
* @return Zend_Controller_Router_Route_Abstract
*/
public function getRoute()
{
if ($this->_route === null) {
$this->_route = new Zend_Controller_Router_Route_Static($this->url_key,
$this->getNavigationData()
);
if ($this->parent) {
$parentRoute = $this->parent->getRoute();
$this->_route = $parentRoute->chain($this->_route);
}
}
return $this->_route;
}
Next, if multilanguage is supported, i prepend all the routes with the language route:
foreach ($front->getRouter()->getRoutes() as $routeName => $route) {
if ($routeName != 'lang') {
$front->getRouter()->addRoute($routeName, $this->_languageRoute->chain($route));
}
}
All the routes are assembled correctly. But once a page has and/or is a child the routes arent matched.
But aside from it not working yet, im also wondering if this is the best approach. The page structure could get pretty big, meaning it would also create alot of route objects. I'm not sure what effect this has on performance.
Alternatively i was thinking about writing a custom router/route to handle all the cms-routes in 1 route. This would also make it easier to assemble cms-urls.
I hope someone can give me some advice on this issue.