public function run() { $di = new \Phalcon\DI\FactoryDefault(); // Config require_once APPLICATION_PATH . '/modules/Cms/Config.php'; $config = \Cms\Config::get(); $di->set('config', $config); // Registry $registry = new \Phalcon\Registry(); $di->set('registry', $registry); // Loader $loader = new \Phalcon\Loader(); $loader->registerNamespaces($config->loader->namespaces->toArray()); $loader->registerDirs([APPLICATION_PATH . "/plugins/"]); $loader->register(); require_once APPLICATION_PATH . '/../vendor/autoload.php'; // Database $db = new \Phalcon\Db\Adapter\Pdo\Mysql(["host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "charset" => $config->database->charset]); $di->set('db', $db); // View $this->initView($di); // URL $url = new \Phalcon\Mvc\Url(); $url->setBasePath($config->base_path); $url->setBaseUri($config->base_path); $di->set('url', $url); // Cache $this->initCache($di); // CMS $cmsModel = new \Cms\Model\Configuration(); $registry->cms = $cmsModel->getConfig(); // Отправляем в Registry // Application $application = new \Phalcon\Mvc\Application(); $application->registerModules($config->modules->toArray()); // Events Manager, Dispatcher $this->initEventManager($di); // Session $session = new \Phalcon\Session\Adapter\Files(); $session->start(); $di->set('session', $session); $acl = new \Application\Acl\DefaultAcl(); $di->set('acl', $acl); // JS Assets $this->initAssetsManager($di); // Flash helper $flash = new \Phalcon\Flash\Session(['error' => 'ui red inverted segment', 'success' => 'ui green inverted segment', 'notice' => 'ui blue inverted segment', 'warning' => 'ui orange inverted segment']); $di->set('flash', $flash); $di->set('helper', new \Application\Mvc\Helper()); // Routing $this->initRouting($application, $di); $application->setDI($di); // Main dispatching process $this->dispatch($di); }
public static function run() { if (in_array(APPLICATION_ENV, array('development'))) { $debug = new \Phalcon\Debug(); $debug->listen(); } $di = new \Phalcon\DI\FactoryDefault(); $config = (include APPLICATION_PATH . '/config/application.php'); $di->set('config', $config); $loader = new \Phalcon\Loader(); $loader->registerNamespaces($config->loader->namespaces->toArray()); $loader->register(); $loader->registerDirs(array(APPLICATION_PATH . "/plugins/")); $db = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "charset" => $config->database->charset)); $di->set('db', $db); $view = new View(); /* $view->disableLevel(array( View::LEVEL_BEFORE_TEMPLATE => true, View::LEVEL_LAYOUT => true, View::LEVEL_AFTER_TEMPLATE => true )); */ define('MAIN_VIEW_PATH', '../../../views/'); $view->setMainView(MAIN_VIEW_PATH . 'main'); $view->setLayoutsDir(MAIN_VIEW_PATH . '/layouts/'); $view->setPartialsDir(MAIN_VIEW_PATH . '/partials/'); $volt = new \Application\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array('compiledPath' => APPLICATION_PATH . '/cache/volt/')); $volt->initCompiler(); $viewEngines = array(".volt" => $volt); $view->registerEngines($viewEngines); $di->set('view', $view); $viewSimple = new \Phalcon\Mvc\View\Simple(); $viewSimple->registerEngines($viewEngines); $di->set('viewSimple', $viewSimple); $url = new \Phalcon\Mvc\Url(); $url->setBasePath('/'); $url->setBaseUri('/'); $application = new \Phalcon\Mvc\Application(); $application->registerModules($config->modules->toArray()); $router = new \Application\Mvc\Router\DefaultRouter(); foreach ($application->getModules() as $module) { $className = str_replace('Module', 'Routes', $module['className']); if (class_exists($className)) { $class = new $className(); $router = $class->init($router); } } $di->set('router', $router); $eventsManager = new \Phalcon\Events\Manager(); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) { new ExceptionPlugin($dispatcher, $exception); }); $eventsManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher) { new LocalizationPlugin($dispatcher); }); $eventsManager->attach("acl", function ($event, $acl) { if ($event->getType() == 'beforeCheckAccess') { echo $acl->getActiveRole(), $acl->getActiveResource(), $acl->getActiveAccess(); } }); $dispatcher->setEventsManager($eventsManager); $di->set('dispatcher', $dispatcher); $session = new \Phalcon\Session\Adapter\Files(); $session->start(); $di->set('session', $session); $acl = new \Application\Acl\DefaultAcl(); $acl->setEventsManager($eventsManager); $di->set('acl', $acl); $assets = new \Phalcon\Assets\Manager(); $di->set('assets', $assets); $flash = new \Phalcon\Flash\Session(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning')); $di->set('flash', $flash); $di->set('helper', new \Application\Mvc\Helper()); $application->setDI($di); echo $application->handle()->getContent(); }
$di->set('config', $config); $di->set('cookies', function () { $cookies = new Phalcon\Http\Response\Cookies(); return $cookies; }); /** * Registering a router */ $di->set('router', require __DIR__ . '/../common/config/routes.php'); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/'); $url->setBasePath(__DIR__); return $url; }); /** * We register the events manager */ $di->set('dispatcher', function () use($di) { $eventsManager = $di->getShared('eventsManager'); /** * if not found page */ /* $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) { if ($event->getType() == 'beforeException')
/** * Created by PhpStorm. * User: lxh * Date: 15-7-13 * Time: 下午2:35 */ ini_set('display_errors', 'On'); error_reporting(E_ALL); date_default_timezone_set('Asia/Shanghai'); try { $loader = new \Phalcon\Loader(); $loader->registerDirs(array('../app/controllers', '../app/models'))->register(); $di = new Phalcon\DI\FactoryDefault(); $di->set('view', function () { $view = new Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); return $view; }); $di->set('url', function () { $url = new Phalcon\Mvc\Url(); $url->setBasePath('/lxh/'); return $url; }); $di->set('db', function () { return new \Phalcon\Db\Adapter\Pdo\Mysql(array('host' => "localhost", 'username' => 'root', 'password' => '123456', 'dbname' => 'test')); }); $application = new Phalcon\Mvc\Application($di); echo $application->handle()->getContent(); } catch (Phalcon\Exception $e) { echo "PhalconException:", $e->getMessage(); }
public function run() { $di = new \Phalcon\DI\FactoryDefault(); $config = (include APPLICATION_PATH . '/config/application.php'); $di->set('config', $config); $registry = new \Phalcon\Registry(); $loader = new \Phalcon\Loader(); $loader->registerNamespaces($config->loader->namespaces->toArray()); $loader->register(); $loader->registerDirs(array(APPLICATION_PATH . "/plugins/")); $db = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "charset" => $config->database->charset)); $di->set('db', $db); $view = new \Phalcon\Mvc\View(); define('MAIN_VIEW_PATH', '../../../views/'); $view->setMainView(MAIN_VIEW_PATH . 'main'); $view->setLayoutsDir(MAIN_VIEW_PATH . '/layouts/'); $view->setPartialsDir(MAIN_VIEW_PATH . '/partials/'); $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array('compiledPath' => APPLICATION_PATH . '/cache/volt/')); $phtml = new \Phalcon\Mvc\View\Engine\Php($view, $di); $viewEngines = array(".volt" => $volt, ".phtml" => $phtml); $registry->viewEngines = $viewEngines; $view->registerEngines($viewEngines); if (isset($_GET['_ajax']) && $_GET['_ajax']) { $view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_LAYOUT); } $di->set('view', $view); $viewSimple = new \Phalcon\Mvc\View\Simple(); $viewSimple->registerEngines($viewEngines); $di->set('viewSimple', $viewSimple); $url = new \Phalcon\Mvc\Url(); $url->setBasePath('/'); $url->setBaseUri('/'); $cacheFrontend = new \Phalcon\Cache\Frontend\Data(array("lifetime" => 60, "prefix" => HOST_HASH)); switch ($config->cache) { case 'file': $cache = new \Phalcon\Cache\Backend\File($cacheFrontend, array("cacheDir" => __DIR__ . "/cache/backend/")); break; case 'memcache': $cache = new \Phalcon\Cache\Backend\Memcache($cacheFrontend, array("host" => "localhost", "port" => "11211")); break; } $di->set('cache', $cache); $di->set('modelsCache', $cache); switch ($config->metadata_cache) { case 'memory': $modelsMetadata = new \Phalcon\Mvc\Model\Metadata\Memory(); break; case 'apc': $modelsMetadata = new \Phalcon\Mvc\Model\MetaData\Apc(array("lifetime" => 60, "prefix" => HOST_HASH)); break; } $di->set('modelsMetadata', $modelsMetadata); /** * CMS Конфигурация */ $cmsModel = new \Cms\Model\Configuration(); $cms = $cmsModel->getConfig(); // @todo Будет отдельный раздел конфигурации для управления языками. Пока заглушка. $cms['languages'] = [['name' => 'Русский', 'iso' => 'ru', 'locale' => 'ru_RU'], ['name' => 'English', 'iso' => 'en', 'locale' => 'en_EN']]; $registry->cms = $cms; // Отправляем в Registry $application = new \Phalcon\Mvc\Application(); $application->registerModules($config->modules->toArray()); $router = new \Application\Mvc\Router\DefaultRouter(); foreach ($application->getModules() as $module) { $className = str_replace('Module', 'Routes', $module['className']); if (class_exists($className)) { $class = new $className(); $router = $class->init($router); } } $di->set('router', $router); $eventsManager = new \Phalcon\Events\Manager(); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $eventsManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher, $di) use($di) { new LocalizationPlugin($dispatcher); new AclPlugin($di->get('acl'), $dispatcher); }); $profiler = new \Phalcon\Db\Profiler(); $eventsManager->attach('db', function ($event, $db) use($profiler) { if ($event->getType() == 'beforeQuery') { $profiler->startProfile($db->getSQLStatement()); } if ($event->getType() == 'afterQuery') { $profiler->stopProfile(); } }); $db->setEventsManager($eventsManager); $di->set('profiler', $profiler); $dispatcher->setEventsManager($eventsManager); $di->set('dispatcher', $dispatcher); $session = new \Phalcon\Session\Adapter\Files(); $session->start(); $di->set('session', $session); $acl = new \Application\Acl\DefaultAcl(); $di->set('acl', $acl); $assets = new \Phalcon\Assets\Manager(); $di->set('assets', $assets); $flash = new \Phalcon\Flash\Session(array('error' => 'ui red inverted segment', 'success' => 'ui green inverted segment', 'notice' => 'ui blue inverted segment', 'warning' => 'ui orange inverted segment')); $di->set('flash', $flash); $di->set('helper', new \Application\Mvc\Helper()); $di->set('registry', $registry); $assetsManager = new \Phalcon\Assets\Manager(); $di->set('assets', $assetsManager); $application->setDI($di); $this->dispatch($di); }