Skip to content

Instantly share code, notes, and snippets.

@nesbert
Created September 11, 2012 20:05
Show Gist options
  • Save nesbert/3701633 to your computer and use it in GitHub Desktop.
Save nesbert/3701633 to your computer and use it in GitHub Desktop.
Working copy of multiple module Phalcon bootstrap (public/index.php).
<?php
/**
* Application driver class to initialize Phalcon and
* other resources.
*/
class Application extends \Phalcon\Mvc\Application
{
private static $mode = 'development'; // TODO change default to production
private static $modules = array(
'portal' => array(
'className' => 'Knode\Portal\Module',
'path' => '../apps/portal/Module.php'
),
'admin' => array(
'className' => 'Knode\Admin\Module',
'path' => '../apps/admin/Module.php'
)
);
const DEFAULT_MODULE = 'portal';
const MODE_PRODUCTION = 'production';
const MODE_STAGING = 'staging';
const MODE_TEST = 'test';
const MODE_DEVELOPMENT = 'development';
/**
* Set application mode and error reporting level.
*/
public function __construct()
{
if (!defined('PHALCON_MODE')) {
$mode = getenv('PHALCON_MODE');
$mode = $mode ? $mode : self::$mode;
define('PHALCON_MODE', $mode);
}
switch (self::getMode()) {
case self::MODE_PRODUCTION:
case self::MODE_STAGING:
error_reporting(0);
break;
case self::MODE_TEST:
case self::MODE_DEVELOPMENT:
error_reporting(E_ALL);
break;
}
}
/**
* Register the services here to make them general or register in
* the ModuleDefinition to make them module-specific.
*/
protected function _registerServices()
{
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
__DIR__ . '/../library/',
__DIR__ . '/../vendor/',
)
)->register();
// Init a DI
$di = new \Phalcon\DI\FactoryDefault();
// Registering a router:
$defaultModule = self::DEFAULT_MODULE;
$modules = self::$modules;
$di->set('router', function() use ($defaultModule, $modules) {
$router = new \Phalcon\Mvc\Router();
$router->setDefaultModule($defaultModule);
foreach ($modules as $moduleName => $module) {
// do not route default module
if ($defaultModule == $moduleName) continue;
$router->add('#^/'.$moduleName.'(|/)$#', array(
'module' => $moduleName,
'controller' => 'index',
'action' => 'index',
));
$router->add('#^/'.$moduleName.'/([a-zA-Z0-9\_]+)[/]{0,1}$#', array(
'module' => $moduleName,
'controller' => 1,
));
$router->add('#^/'.$moduleName.'[/]{0,1}([a-zA-Z0-9\_]+)/([a-zA-Z0-9\_]+)(/.*)*$#', array(
'module' => $moduleName,
'controller' => 1,
'action' => 2,
'params' => 3,
));
}
return $router;
});
$this->setDI($di);
}
public function main()
{
try {
$this->_registerServices();
$this->registerModules(self::$modules);
$this->handle()->send();
} catch (Exception $e) {
// TODO log exception
// remove view contents from buffer
ob_clean();
$errorCode = 500;
$errorView = 'errors/500_error.phtml';
switch (true) {
// 401 UNAUTHORIZED
case $e->getCode() == 401:
$errorCode = 401;
$errorView = 'errors/401_unathorized.phtml';
break;
// 403 FORBIDDEN
case $e->getCode() == 403:
$errorCode = 403;
$errorView = 'errors/403_forbidden.phtml';
break;
// 404 NOT FOUND
case $e->getCode() == 404:
case ($e instanceof Phalcon\Mvc\View\Exception):
case ($e instanceof Phalcon\Mvc\Dispatcher\Exception):
$errorCode = 404;
$errorView = 'errors/404_not_found.phtml';
break;
}
// Get error view contents. Since we are including the view
// file here you can use PHP and local vars inside the error view.
ob_start();
include_once $errorView;
$contents = ob_get_contents();
ob_end_clean();
// send view to header
$response = $this->getDI()->getShared('response');
$response->resetHeaders()
->setStatusCode($errorCode, null)
->setContent($contents)
->send();
}
}
public static function getMode()
{
return self::$mode;
}
}
// Run application:
$app = new Application();
$app->main();
@odiel
Copy link

odiel commented Nov 9, 2012

I'm trying to do something similar and your code help me a lot.

Thanks.

@hugoduraes
Copy link

Thank you for this. It helped me a lot! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment