root/public/index.php
<?php
use Phalcon\Mvc\Application;
define('APP_PATH', realpath('..'));
try {
$config = require APP_PATH . "/config/config.php";
/**
* Include services
*/
require APP_PATH . '/config/services.php';
/**
* Handle the request
*/
$application = new Application($di);
/**
* Include modules
*/
require APP_PATH . '/config/modules.php';
// Handle the application:
echo $application->handle()->getContent();
}
catch (\Exception $e) {
echo $e->getMessage() . '<br>';
echo '<pre>' . $e->getTraceAsString() . '</pre>';
}
root/config/config.php
<?php
return new \Phalcon\Config(array(
'database' => array(
...
),
'application' => array(
'controllersDir' => __DIR__ . '/../controllers/',
'modelsDir' => __DIR__ . '/../models/',
'migrationsDir' => __DIR__ . '/../migrations/',
'viewsDir' => __DIR__ . '/../views/',
'baseUri' => '/',
'cacheDir' => APP_PATH . '/cache/',
)
));
root/config/services.php
<?php
/**
* Services are globally registered in this file
*
* @var \Phalcon\Config $config
*/
use Phalcon\Loader;
use Phalcon\Mvc\Router;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Di\FactoryDefault;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Flash\Direct as FlashDirect;
use Phalcon\Flash\Session as FlashSession;
/**
* The FactoryDefault Dependency Injector automatically registers the right services to provide a full stack framework
*/
$di = new FactoryDefault();
/**
* Create a loader to register all files.
*/
$loader = new Loader();
$loader->registerNamespaces(array(
'Apps' => APP_PATH . '/Apps/',
));
$loader->register();
/**
* Registering a router
*/
$di->setShared('router', function () {
$useSlugExtension = true;
$router = new \Apps\Source\Plugins\Routing\Router($useSlugExtension);
// $router = new \Apps\Source\Plugins\Routing\Router();
$router->setDefaultModule('frontend');
$router->setDefaultNamespace('\Apps\Modules\Frontend\Controllers');
return $router;
});
/**
* The URL component is used to generate all kinds of URLs in the application
*/
$di->setShared('url', function () use ($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
});
/**
* Setting up the view component
*/
$di->setShared('view', function () use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array(
'.volt' => function ($view, $di) use ($config) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir,
'compiledSeparator' => '_'
));
return $volt;
},
'.phtml' => function ($view, $di) use ($config) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir,
'compiledSeparator' => '_'
));
// Add functions to the compiler:
$volt->getCompiler()->addFunction(
'in_array', 'in_array'
);
return $volt;
}
));
return $view;
});
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->setShared('db', function () use ($config) {
$dbConfig = $config->database->toArray();
$adapter = $dbConfig['adapter'];
unset($dbConfig['adapter']);
$class = 'Phalcon\Db\Adapter\Pdo\\' . $adapter;
return new $class($dbConfig);
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->setShared('modelsMetadata', function () {
return new MetaDataAdapter();
});
/**
* Starts the session the first time some component requests the session service
*/
$di->setShared('session', function () {
$session = new SessionAdapter();
$session->start();
return $session;
});
/**
* Add authentication management
*/
$di->setShared('auth', function() use ($di) {
$session = $di->get('session');
$auth = \Apps\Source\Components\AuthManager::getInstance($session);
return $auth;
});
/**
* Date manager
*/
$di->setShared('date', function(){
return new \Apps\Source\Plugins\Date\Manager();
});
/**
* Register the direct flash service with the Twitter Bootstrap classes
*/
$di->set('flash', function () {
return new FlashDirect(
array(
'error' => 'alert alert-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
'warning' => 'alert alert-warning'
)
);
});
/**
* Register the session flash service with the Twitter Bootstrap classes
*/
$di->set('flashSession', function () {
return new FlashSession(
array(
'error' => 'alert alert-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
'warning' => 'alert alert-warning'
)
);
});
/**
* Set the default namespace for dispatcher
*/
$di->setShared('dispatcher', function() use ($di) {
$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) {
//Handle 404 exceptions
if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) {
$dispatcher->forward(array(
'namespace' => 'Apps\Modules\Frontend\Controllers',
'module' => 'frontend',
'controller' => 'error',
'action' => 'show404'
));
return false;
}
});
$dispatcher = new \Phalcon\Mvc\Dispatcher();
//Bind the EventsManager to the dispatcher
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});