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 wildcard in controller in a multiModule application

In the below route, I want to get the admin wildcard within the controller function.

$router->add(
"/admin/:controller/a/:action/:params",
[
    "controller" => 1,
    "action"     => 2,
    "params"     => 3,
]
);

Hi @Dominic in a multimodule app you must setup the module in the route

$router->add(
"/admin/:controller/a/:action/:params",
[
    "module" => "admin", //or your module name
    "controller" => 1,
    "action"     => 2,
    "params"     => 3,
]
);

This only works with urls like /admin/mycontroller/a/action (params are optional). If you want route /admin/mycontroller you have to

$router->add(
"/admin/:controller",
[
    "module" => "admin", //or your module name
    "action" => "index", //or your default action name
    "controller" => 1,    
]
);

Full routing documentation

Good luck

Thanks for a response but I think you didnt understand me.

I have already set the routes but I want to share a module for different wildcards. i.e wild card admin, manager Which I have no issue with.

My concern is on Controller action I want to get the wildcard so I can identify if it is admin or manager. so far I can get the module,controller,action, params. But am interested on that wildcard section.

Hi @Dominic in a multimodule app you must setup the module in the route

$router->add(
"/admin/:controller/a/:action/:params",
[
   "module" => "admin", //or your module name
   "controller" => 1,
   "action"     => 2,
   "params"     => 3,
]
);

This only works with urls like /admin/mycontroller/a/action (params are optional). If you want route /admin/mycontroller you have to

$router->add(
"/admin/:controller",
[
   "module" => "admin", //or your module name
  "action" => "index", //or your default action name
   "controller" => 1,    
]
);

Full routing documentation

Good luck



32.2k
Accepted
answer

so you have to use and extra param in your url like

$router->add("/admin/(administrator|manager)/:cotroller",
[
'user-role' => 1,
'controller' => 2,
]
);

then in your controller

public function someAction() {
echo $this->dispatcher->getParam('user-role');
}

Good luck

With a little modification I achieved what I wanted. Great

so you have to use and extra param in your url like

$router->add("/admin/(administrator|manager)/:cotroller",
[
'user-role' => 1,
'controller' => 2,
]
);

then in your controller

public function someAction() {
echo $this->dispatcher->getParam('user-role');
}

Good luck