Im attempting to use the multi module configuration for the Phalcon PHP Framework.
Im having an issue relating to the routing as one of my modules is inaccessible via url. It seems every time I try to access the defined modules uri, it tries to invoke the default modules namespace. Project\Module1\Controllers\Module2Controller handler class cannot be loaded
The idea solution
url.com/module1 => module1
url.com/moudle2 => module2
url.com => module1
In working to resolve this problem, I have verified that all paths and namespaces are correct. Ive combed through it with xdebug.
Ive created my project with Phalcon Devtools with the multi module type defined as it provided me with a skeleton to follow. As I am using this method, it provided a default routing method that interprets each registered module and binds it to a url. This does not seem to be working for me. This is the Phalcon project before interpretation. https://github.com/phalcon/phalcon-devtools/tree/master/templates/project/modules
Routing
https://github.com/phalcon/phalcon-devtools/blob/master/templates/project/modules/routes.php
$router = $di->get("router");
foreach ($application->getModules() as $key => $module) {
$namespace = str_replace('Module','Controllers', $module["className"]);
$router->add('/'.$key.'/:params', array(
'namespace' => $namespace,
'module' => $key,
'controller' => 'index',
'action' => 'index',
'params' => 1
))->setName($key);
$router->add('/'.$key.'/:controller/:params', array(
'namespace' => $namespace,
'module' => $key,
'controller' => 1,
'action' => 'index',
'params' => 2
));
$router->add('/'.$key.'/:controller/:action/:params', array(
'namespace' => $namespace,
'module' => $key,
'controller' => 1,
'action' => 2,
'params' => 3
));
}
Module Registration
https://github.com/phalcon/phalcon-devtools/blob/master/templates/project/modules/modules.php
$application->registerModules(array(
'module1' => array(
'className' => 'Project\Module1\Module',
'path' => __DIR__ . '/../apps/module1/Module.php'
),
'module2' => array(
'className' => 'Project\Module2\Module',
'path' => __DIR__ . '/../apps/module2/Module.php'
)
));
Service
https://github.com/phalcon/phalcon-devtools/blob/master/templates/project/modules/services.php
$di->set('router', function () {
$router = new Router();
$router->setDefaultModule('module1');
$router->setDefaultNamespace('Project\Module1\Controllers');
return $router;
});
An example provided by Phalcon. This not exact to the Phalcon Tools implementation.
https://github.com/phalcon/mvc/tree/master/multiple-factory-default
Any help is much appreciated.
I have my general routes defined in app\configoutes.php
then in app\configoutes
I have one file for each of the groups (in this case modules).
app\configoutes.php
<?php
$router = new \Phalcon\Mvc\Router();
$router->setDefaultModule("frontend");
$router->add("/:module/:controller/:action/:params", array(
'module' => 1,
'controller' => 2,
'action' => 3,
'params' => 4
));
foreach (glob(__DIR__ . "/routes/*.php") as $filename) {
include $filename;
}
$router->removeExtraSlashes(true);
return $router;
admin\configoutes\admin.php
<?php
use Phalcon\Mvc\Router\Group;
//Create a group with a common module and controller
$admin = new Group(array('module' => 'admin'));
$admin->setPrefix('/admin');
$admin->add("", array(
'controller' => 'index',
'action' => 'index'
));
$admin->add("/foo", array(
'controller' => 'foo',
'action' => 'index'
));
$admin->add("/bar", array(
'controller' => 'bar',
'action' => 'index'
));
$router->mount($admin);
I have only had to define the controller and index because any other actions will be part of the url and should route by themselves.
index.php
/*
* ... includes
*/
$application = new \Phalcon\Mvc\Application($di);
$application->registerModules(array(
'admin' => array(
'className' => 'Calmsplash\App\Admin',
'path' => '../app/admin/Admin.php',
),
'frontend' => array(
'className' => 'Calmsplash\App\Frontend',
'path' => '../app/frontend/Frontend.php',
),
/// ... more modules
));
I've called my Module
files after the module name just to fit them into the namespace.