hey, my code: // or docs here: https://docs.phalcon.io/en/latest/reference/routing.html#groups-of-routes
application.php
$this->di->set('router', new ApplicationRouter(), true);
//call this function after you are done registering services
public function _registerModules($modules, $merge = null) {
parent::registerModules($modules, $merge);
$loader = new Loader();
$modules = $this->getModules();
/**
* Iterate the application modules and register the routes
* by calling the initRoutes method of the Module class.
* We need to auto load the class
*/
foreach ($modules as $module) {
$className = $module['className'];
if (!class_exists($className, false)) {
$loader->registerClasses([ $className => $module['path'] ], true)->register()->autoLoad($className);
}
$className::initRoutes($this->di);
}
}
ApplicationRouter.php
class ApplicationRouter extends Router
{
/**
* Creates a new instance of ApplicationRouter class and defines standard application routes
* @param boolean $defaultRoutes
*/
public function __construct($defaultRoutes = false)
{
parent::__construct($defaultRoutes);
$this->removeExtraSlashes(true);
/**
* Controller and action always default to 'index'
*/
$this->setDefaults([
'controller' => 'index',
'action' => 'index'
]);
$this->add('/', array(
'module' => 'home',
'controller' => 'index',
'action' => 'index',
'namespace' => '\Home\Controllers\\'
))->setName('default');
$this->add('/logout', array(
'module' => 'home',
'controller' => 'index',
'action' => 'logout',
'namespace' => '\Home\Controllers\\'
))->setName('default-logout');
/**
* Add default not found route
*/
$this->notFound([
'module' => 'home',
'controller' => 'index',
'action' => 'notFound',
'namespace' => '\Home\Controllers\\'
]);
}
}
my Module.php
public static function initRoutes(\Phalcon\DiInterface $di) {
$loader = new Loader();
$loader->registerNamespaces([
'\Home' => __DIR__,
], true)
->register();
$router = $di->getRouter();
$router->mount(new ModuleRoutes());
}
ModuleRoutes.php
class ModuleRoutes extends \Phalcon\Mvc\Router\Group
{
/**
* Initialize the router group for the Home module
*/
public function initialize() {
$this->setPrefix('/home');
/**
* Configure the instance
*/
$this->setPaths([
'module' => 'home',
'controller' => 'index',
'action' => 'index'
]);
/**
* Default route: 'home-root'
*/
$this->addGet('', [])
->setName('home-root');
/**
* Controller route: 'home-controller'
*/
$this->addGet('/:controller', ['controller' => 1])
->setName('home-controller');
/**
* Action route: 'home-action'
*/
$this->addGet('/:controller/:action/:params', [
'controller' => 1,
'action' => 2,
'params' => 3
])
->setName('home-action');
}
}