We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Get all route list name

Hello there. I want to get the whole route list. I try this, but it does not work;

$this->router->getRoutes()

That's exactly what I want to do;

$allRoutes = $this->router->getRoutes();

$allRoutes[0]->name;

How can I do that ? Screenshot;



2.3k
Accepted
answer
edited Dec '16

I finally did it. It's very easy. :)

$allRoutes = $this->router->getRoutes();

foreach ($allRoutes as $key => $value) {
    $ref = new \ReflectionClass($value);
    $classProperty = $ref->getProperty('_name');
    $classProperty->setAccessible(true);
    $data = $classProperty->getValue($value);

    var_dump($data);
}

public function getRoutes()
{
    $routes = [];

    $router = $this->getDI()->get('router');
    foreach ($router->getRoutes() as $route) {
        /** @var \Phalcon\Mvc\Router\Route $route */
        $paths = $route->getPaths();

        $pathsString = '';

        if (isset($paths['module'])) {
            $pathsString .= $paths['module'];
            $module = $paths['module'];
        } else {
            $module = '';
        }

        if (isset($paths['namespace'])) {
            $pathsString .= '\\' . ltrim($paths['namespace'], '\\');
        }

        if (isset($paths['controller'])) {
            $pathsString .= '\\' . \Phalcon\Text::camelize(ltrim($paths['controller'], '\\'));
        }

        if (isset($paths['action'])) {
            $pathsString .= '::' . $paths['action'];
        }

        $routes[$route->getRouteId()] = (object) [
            'name'             => $route->getName(),
            'http_methods'     => $route->getHttpMethods(),
            'host_name'        => $route->getHostname(),
            'pattern'          => $route->getPattern(),
            'before_match'     => $route->getBeforeMatch(),
            'compiled_pattern' => $route->getCompiledPattern(),
            'group'            => $route->getGroup(),
            'converters'       => $route->getConverters(),
            'paths'            => $pathsString,
            'module'           => $module,
        ];
    }

    return $routes;
}