For source code: https://github.com/OkveeNet/phalcon-begins
I have 2 modules.
- is core and 2. is contact
This is the Module.php file for core module.
<?php
/*
*
* @author Vee W.
* @license https://opensource.org/licenses/MIT
*
*/
namespace Core;
use Phalcon\Loader,
Phalcon\Mvc\Dispatcher,
Phalcon\Mvc\View,
Phalcon\Mvc\View\Engine\Volt,
Phalcon\Mvc\ModuleDefinitionInterface;
class Module implements ModuleDefinitionInterface
{
/**
* Register a specific autoloader for the module
*/
public function registerAutoloaders()
{
$loader = new Loader();
$loader->registerNamespaces(
array(
'Core\\Controllers' => __DIR__.'/controllers/',
'Core\\Models' => __DIR__.'/models/',
)
);
$loader->register();
}// registerAutoloaders
/**
* Register specific services for the module
*/
public function registerServices($di)
{
$config = include APPFULLPATH.'/config/config.php';
$dispatcher = $di->getShared('dispatcher');
// Registering a dispatcher
$di->set('dispatcher', function() use($di, $dispatcher) {
$dispatcher->setDefaultNamespace("Core\\Controllers");
return $dispatcher;
});
// Registering the view component
$di->set('view', function() use ($config) {
$view = new View();
$view->setViewsDir(__DIR__.'/views/');
$view->registerEngines(array(
'.volt' => function ($view, $di) use ($config) {
$volt = new Volt($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir,
'compiledSeparator' => '_'
));
return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php',
'.php' => 'Phalcon\Mvc\View\Engine\Php',
));
return $view;
});
}
}
And this is Module.php file for contact module.
<?php
/*
*
* @author Vee W.
* @license https://opensource.org/licenses/MIT
*
*/
namespace Modules\Contact;
use Phalcon\Loader,
Phalcon\Mvc\Dispatcher,
Phalcon\Mvc\View,
Phalcon\Mvc\View\Engine\Volt,
Phalcon\Mvc\ModuleDefinitionInterface;
class Module implements ModuleDefinitionInterface
{
/**
* Register a specific autoloader for the module
*/
public function registerAutoloaders()
{
$loader = new Loader();
$loader->registerNamespaces(
array(
'Modules\\Contact\\Controllers' => __DIR__.'/controllers/',
'Modules\\Contact\\Models' => __DIR__.'/models/',
'Core\\Controllers' => APPFULLPATH.'/controllers/',
)
);
$loader->register();
}// registerAutoloaders
/**
* Register specific services for the module
*/
public function registerServices($di)
{
$config = include APPFULLPATH.'/config/config.php';
// Registering a dispatcher
$di->set('dispatcher', function() use ($di) {
$evManager = $di->getShared('eventsManager');
$evManager->attach('dispatch:beforeException', function($event, $dispatcher, $exception) {
switch ($exception->getCode()) {
case PhDispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case PhDispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(
array(
'module' => 'core',
'controller' => 'error',
'action' => 'e404',
)
);
return false;
}
}, true);
$dispatcher = new Dispatcher;
$dispatcher->setEventsManager($evManager);
$dispatcher->setDefaultNamespace('Modules\\Contact\\Controllers');
return $dispatcher;
});
// Registering the view component
$di->set('view', function() use ($config) {
$view = new View();
$view->setViewsDir(__DIR__.'/views/');
$view->registerEngines(array(
'.volt' => function ($view, $di) use ($config) {
$volt = new Volt($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir,
'compiledSeparator' => '_'
));
return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php',
'.php' => 'Phalcon\Mvc\View\Engine\Php',
));
return $view;
});
}// registerServices
}
I must register service in all modules i have which is very inconvenient and hard to make change later. If it uses only main bootstrap it is easier to manage. For example, make change on view component, change dispatcher settings to show 404 page.
How to use shared main bootstrap in Phalcon php?