Skip to content

Instantly share code, notes, and snippets.

@Freeaqingme
Created March 17, 2013 01:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Freeaqingme/5179181 to your computer and use it in GitHub Desktop.
Save Freeaqingme/5179181 to your computer and use it in GitHub Desktop.
Generate sitemap with #ZF2
<?php
$sm = $e->getApplication()->getServiceManager();
$routes = $sm->get('config')['router']['routes'];
echo '<ul>' . $this->parseRoutes($routes) . '</ul>';
protected function parseRoutes($routes, $names = array(), $url = array())
{
echo '<ul>';
foreach ($routes as $name => $route) {
echo '<li>';
if (isset($route['options'])) {
echo '<a href="' . implode($url). $route['options']['route'] . '">' . $name . '</a>';
}
if (isset($route['child_routes'])) {
echo $this->parseRoutes($route['child_routes'], $names + array($name), array_merge($url, array($route['options']['route'])));
}
echo '</li>';
}
echo '</ul>';
}
@iit2011081
Copy link

iit2011081 commented May 29, 2017

From my understanding I wrote following content in my phtml file.

<?php
        $sm = $e->getApplication()->getServiceManager();
        $routes = $sm->get('config')['router']['routes'];
        echo '<ul>' . $this->parseRoutes($routes) . '</ul>';

and I wrote the below content in my controller

protected function parseRoutes($routes, $names = array(), $url = array())
    {
        echo '<ul>';
        foreach ($routes as $name => $route) {
            echo '<li>';
            if (isset($route['options'])) {
                echo '<a href="' . implode($url). $route['options']['route'] . '">' . $name . '</a>';
            }
            if (isset($route['child_routes'])) {
                echo $this->parseRoutes($route['child_routes'], $names + array($name), array_merge($url, array($route['options']['route'])));
            }
            echo '</li>';
        }
        echo '</ul>';
    }

But it is giving error undefined variable e for this line $sm = $e->getApplication()->getServiceManager(); .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment