Hi,
I created a new phalcon project with devtools:
phalcon project box_wcs --type=modules
The project has been created with the standard module 'frontend'. I want to add another module named 'backend' and used therefore this command:
phalcon module --name backend --namespace=Box_wcs\ --output apps/
This created a new module named 'backend' inside the apps directory
After that I edited the apps/backend/Module.php, config/modules.php and the config/routes.php
The apps/backend/Modules.php looks like the following:
<?php
namespace Box_wcs\Backend;
use Phalcon\DiInterface;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\ModuleDefinitionInterface;
use Phalcon\Config;
class Module implements ModuleDefinitionInterface
{
/**
* Registers an autoloader related to the module
*
* @param DiInterface $di
*/
public function registerAutoloaders(DiInterface $di = null)
{
$loader = new Loader();
$loader->registerNamespaces(array(
'Box_wcs\Backend\Controllers' => __DIR__ . '/controllers/',
'Box_wcs\Backend\Models' => __DIR__ . '/models/'
));
$loader->register();
}
/**
* Registers services related to the module
*
* @param DiInterface $di
*/
public function registerServices(DiInterface $di)
{
/**
* Read common configuration
*/
$config = $di->has('config') ? $di->getShared('config') : null;
/**
* Try to load local configuration
*/
if (file_exists(__DIR__ . '/config/config.php')) {
$override = new Config(include __DIR__ . '/config/config.php');;
if ($config instanceof Config) {
$config->merge($override);
} else {
$config = $override;
}
}
//Registering a dispatcher
$di['dispatcher'] = function () use ($di) {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("Box_wcs\Backend\Controllers");
// $dispatcher->setEventsManager($di->get('eventsManager'));
return $dispatcher;
};
/**
* Setting up the view component
*/
$di['view'] = function () use ($config) {
$view = new View();
$view->setViewsDir($config->get('application')->viewsDir);
return $view;
};
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di['db'] = function () use ($config) {
$config = $config->database->toArray();
$dbAdapter = '\Phalcon\Db\Adapter\Pdo\\' . $config['adapter'];
unset($config['adapter']);
return new $dbAdapter($config);
};
}
}
The config/modules.php:
<?php
/**
* Register application modules
*/
$application->registerModules(array(
'frontend' => array(
'className' => 'Box_wcs\Frontend\Module',
'path' => __DIR__ . '/../apps/frontend/Module.php'
),
'backend' => array(
'className' => 'Box_wcs\Backend\Module',
'path' => __DIR__ . '/../apps/backend/Module.php'
)
));
And the config/routes.php
<?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
));
$router->add('/'.$key.'/:controller/:action', array(
'namespace' => $namespace,
'module' => $key,
'controller' => 1,
'action' => 2
));
}
$di->set("router", $router);
I also creted an IndexController in apps/backend/controllers and a index.phtml in apps/backend/views/index.
But everytime I invoke https://localhost/box_wcs/backend I cannot see anything, just a blank page. https://localhost/box_wcs or https://localhost/box_wcs/frontend work both fine.
Do you have any hint for me?