PHP code example of serebro / phalcon-service-loader

1. Go to this page and download the library: Download serebro/phalcon-service-loader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

serebro / phalcon-service-loader example snippets



	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();


	return new \Phalcon\Config([
		'config' => new \Phalcon\Config([
			'adminEmail' => '[email protected]'
		]),
		'loader' => [
			// ...
		],
		'logger' => [
			// ...
			'shared' => false,
		],
		'cache' => function($di) {
			// ...
			return $cache;
		}
	]);

	'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',
                ]],
            ]],
        ],
    ],