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 route prefix or name from controller and action names

Hello All,

I'm trying to build a menu using the router. My controller name and url aren't the same. How can I get the route pattern or slug from the controller and action name ?

route example:

name = /products

Controller = productManagement

Action = listProducts

I want to get "/products" simple by providing "productManagement/listProducts"

thanks

This is what I'm doing in Micro, it's little bit different for MVC, but it should give you idea (i.e. simply get the Router object where you need it)

//Returns the internal router used by the Micro application, and route name via matched route
    $routeName = $app->getRouter()->getMatchedRoute()->getName();
edited Jun '17
$router->addGet('/products',  ['controller' => 'productManagement', 'action' => 'listProducts'])->setName('/products');

And use code above. Just get router from di(or controller).



3.6k

This code returns the routename of the current controller/action. I'm trying to get the route name for any given controller/action.

This is what I'm doing in Micro, it's little bit different for MVC, but it should give you idea (i.e. simply get the Router object where you need it)

//Returns the internal router used by the Micro application, and route name via matched route
   $routeName = $app->getRouter()->getMatchedRoute()->getName();


3.6k

This code is for setting a route name given a controller/action, I'm trying to get the route name given a controller/action.

$router->addGet('/products',  ['controller' => 'productManagement', 'action' => 'listProducts'])->setName('/products');

And use code above. Just get router from di(or controller).

Then just:

foreach($router->getRoutes() as $route) {
    $route->getName();
}