Service loader for Phalcon PHP Framework
{
"require": {
"serebro/phalcon-service-loader": "dev-master"
}
}
<?php
defined('APP_PATH') || define('APP_PATH', dirname(__FILE__) . '/../app');
defined('WEB_PATH') || define('WEB_PATH', dirname(__FILE__));
defined('ENV') || define('ENV', getenv('ENV') ? getenv('ENV') : 'development');
$services = APP_PATH . '/config/services.php'; // OR $services = APP_PATH . '/config/' . ENV . '.php';
//Create a DI
$di = new Phalcon\DI\FactoryDefault();
// Service loading
$serviceLoader = new \Phalcon\DI\Service\Loader($di);
$serviceLoader->setDefinitions($services, ['loader', 'env']);
//Handle the request
$app = new \Phalcon\Mvc\Application($di);
echo $app->handle()->getContent();
<?php
return new \Phalcon\Config([
'config' => new \Phalcon\Config([
'adminEmail' => 'admin@example.com'
]),
'loader' => [
// ...
],
'logger' => [
// ...
'shared' => false,
],
'cache' => function($di) {
// ...
return $cache;
}
]);
See more "DI - complex registration"
#####Reserved
- "shared" - register the service as "always shared". Default: true.
'loader' => [
'className' => '\Phalcon\Loader',
'calls' => [
['method' => 'registerDirs', 'arguments' => [
['type' => 'parameter', 'value' => [
'controllers' => APP_PATH . '/controllers/',
'models' => APP_PATH . '/models/',
'library' => APP_PATH . '/library/',
]]
]],
['method' => 'register'],
],
],
'env' => function($di) {
error_reporting(0);
ini_set('log_errors', 1);
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
},
'logger' => [
'className' => '\Phalcon\Logger\Adapter\Syslog',
'arguments' => [
['type' => 'parameter', 'value' => null],
],
],
'db' => [
'className' => '\Phalcon\Db\Adapter\Pdo\Mysql',
'arguments' => [
['type' => 'parameter', 'value' => [
'host' => 'localhost',
'username' => 'root',
'password' => 'secret',
'dbname' => 'test_db'
]],
],
],
'cache' => [
'className' => '\Phalcon\Cache\Backend\Memcache',
'arguments' => [
[
'type' => 'instance',
'className' => '\Phalcon\Cache\Frontend\Data',
'arguments' => ['lifetime' => 60]
],
],
],
'fileCache' => [
'className' => '\Phalcon\Cache\Backend\File',
'arguments' => [[
'type' => 'instance',
'className' => '\Phalcon\Cache\Frontend\Data',
'arguments' => ['lifetime' => 3600]
],[
'type' => 'parameter',
'value' => ['cacheDir' => APP_PATH . '/../cache/files/']
],
],
],
'log' => [
'className' => '\Phalcon\Logger\Adapter\File',
'arguments' => [
['type' => 'parameter', 'value' => APP_PATH . '/../logs/app.log'],
],
],
'session' => [
'className' => '\Phalcon\Session\Adapter\Files',
'calls' => [
['method' => 'start']
],
],
'cookie' => [
'className' => '\Phalcon\Http\Response\Cookies',
'calls' => [[
'method' => 'useEncryption',
'arguments' => [
['type' => 'parameter', 'value' => true],
]],
],
],
'url' => [
'className' => '\Phalcon\Mvc\Url',
'calls' => [
['method' => 'setBaseUri', 'arguments' => [
['type' => 'parameter', 'value' => '/'],
]],
],
],
'view' => [
'className' => '\Phalcon\Mvc\View',
'calls' => [
['method' => 'setViewsDir', 'arguments' => [
['type' => 'parameter', 'value' => APP_PATH . '/views/'],
]],
['method' => 'registerEngines', 'arguments' => [
['type' => 'parameter', 'value' => [
'.phtml' => 'Phalcon\Mvc\View\Engine\Php',
]],
]],
],
],