public function registerServices($di) { /** Read configuration */ $config = (include __DIR__ . "/../configs/config.php"); $di['dispatcher'] = function () use($di) { $eventsManager = new EventsManager(); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultNamespace("Modules\\Users\\Controllers"); $eventsManager->attach('dispatch:beforeException', new NotFoundPlugin($di)); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }; /** Setting up the view component */ $di['view'] = function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/views/'); $view->setLayoutsDir('../../common/layouts/'); $view->setPartialsDir('../../common/partials/'); $view->setTemplateAfter('users'); return $view; }; /** Database connection is created based in the parameters defined in the configuration file */ $di['db'] = function () use($config) { return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name, "charset" => "utf8", "options" => array(\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"))); }; }
/** * Register specific services for the module * * @package base-app * @version 2.0 * * @param object $di dependency Injector * * @return void */ public function registerServices(\Phalcon\DiInterface $di) { //Registering a dispatcher $di->set('dispatcher', function () { //Create/Get an EventManager $eventsManager = new \Phalcon\Events\Manager(); //Attach a listener $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) { //controller or action doesn't exist if ($event->getType() == 'beforeException') { switch ($exception->getCode()) { case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array('controller' => 'index', 'action' => 'notFound')); return false; } } }); $dispatcher = new \Phalcon\Mvc\Dispatcher(); //Set default namespace to documentation module $dispatcher->setDefaultNamespace("Baseapp\\Documentation\\Controllers"); //Bind the EventsManager to the dispatcher $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); //Registering the view component $di->set('view', function () use($di) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/views/'); $view->registerEngines(\Baseapp\Library\Tool::registerEngines($view, $di)); return $view; }); }
/** * @param \Phalcon\DiInterface $di */ public function registerServices(\Phalcon\DiInterface $di = null) { // Set up MVC dispatcher. $controller_class = 'Modules\\' . $this->_module_class_name . '\\Controllers'; $di['dispatcher'] = function () use($controller_class) { $eventsManager = new \Phalcon\Events\Manager(); $eventsManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher) { // Set odd/even pairs as the key and value of parameters, respectively. $keyParams = array(); $params = $dispatcher->getParams(); foreach ($params as $position => $value) { if (is_int($position)) { if ($position & 1) { $keyParams[$params[$position - 1]] = urldecode($value); } } else { // The "name" parameter is internal to routes. if ($position != 'name') { $keyParams[$position] = urldecode($value); } } } $dispatcher->setParams($keyParams); // Detect filename in controller and convert to "format" parameter. $controller_name = $dispatcher->getControllerName(); if (strstr($controller_name, '.') !== false) { list($controller_clean, $format) = explode('.', $controller_name, 2); $dispatcher->setControllerName($controller_clean); $dispatcher->setParam('format', $format); } // Detect filename in action and convert to "format" parameter. $action_name = $dispatcher->getActionName(); if (strstr($action_name, '.') !== false) { list($action_clean, $format) = explode('.', $action_name, 2); $dispatcher->setActionName($action_clean); $dispatcher->setParam('format', $format); } }); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($eventsManager); $dispatcher->setDefaultNamespace($controller_class); return $dispatcher; }; // Set up module-specific configuration. $module_base_name = strtolower($this->_module_class_name); $module_config = $di->get('module_config'); $di->setShared('current_module_config', function () use($module_base_name, $module_config) { if (isset($module_config[$module_base_name])) { return $module_config[$module_base_name]; } else { return null; } }); // Set up the view component and shared templates. $views_dir = 'modules/' . $module_base_name . '/views/scripts/'; $di['view'] = function () use($views_dir) { return \FA\Phalcon\View::getView(array('views_dir' => $views_dir)); }; }
public static function initDispatcher() { //Create an EventsManager $eventsManager = new \Phalcon\Events\Manager(); $dispatcher = new \Phalcon\Mvc\Dispatcher(); //Bind the EventsManager to the dispatcher $dispatcher->setEventsManager($eventsManager); return $dispatcher; }
/** * * Register the services here to make them module-specific * */ public function registerServices($di) { // get bootstrap obj $bootstrap = $di->get('bootstrap'); // get config class name $confClass = $bootstrap->getConfClass(); // module config $mConfPath = __DIR__ . '/confs/' . PHALCON_ENV . '.' . PHALCON_CONF_TYPE; if (!is_file($mConfPath)) { throw new \Phalcon\Config\Exception("Module config file not exist, file position: {$mConfPath}"); } if (PHALCON_CONF_TYPE == 'ini') { $mConfig = new $confClass($mConfPath); } else { if (PHALCON_CONF_TYPE == 'php') { $mConfig = new $confClass(require_once $mConfPath); } } // global config $gConfig = $di->get('config'); // merge module config and global config, module's will override global's $gConfig->merge($mConfig); // set config back $di->set('config', $gConfig); // registering a dispatcher $di->set('dispatcher', function () use($di) { $evtManager = $di->getShared('eventsManager'); $evtManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) { switch ($exception->getCode()) { case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array('module' => 'sample', 'controller' => 'error', 'action' => 'show404')); return false; } }); $acl = new \BullSoft\Sample\Plugins\Acl($di, $evtManager); $evtManager->attach('dispatch', $acl); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($evtManager); $dispatcher->setDefaultNamespace("BullSoft\\Sample\\Controllers\\"); return $dispatcher; }); // set view with volt $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/views/'); $view->registerEngines(array(".volt" => function ($view, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array("compiledPath" => $di->get('config')->view->compiledPath, "compiledExtension" => $di->get('config')->view->compiledExtension, "compileAlways" => (bool) $di->get('config')->application->debug)); $compiler = $volt->getCompiler(); $compiler->addExtension(new \BullSoft\Volt\Extension\PhpFunction()); return $volt; })); return $view; }); }
/** * Registra os serviços específicos para este módulo */ public function registerServices($di) { //Registra o dispatcher $di->set('dispatcher', function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); $eventManager = new \Phalcon\Events\Manager(); $eventManager->attach('dispatch', new \Acl('backend')); $dispatcher->setEventsManager($eventManager); $dispatcher->setDefaultNamespace("Multiple\\Backend\\Controllers\\"); return $dispatcher; }); //Registra os diretórios das views $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../apps/backend/views/'); return $view; }); }
/** * Register the services here to make them general or register in the ModuleDefinition to make them module-specific */ public function registerServices($di) { //Registering a dispatcher $di->set('dispatcher', function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); //Attach a event listener to the dispatcher $eventManager = new \Phalcon\Events\Manager(); $eventManager->attach('dispatch', new \Acl('frontend')); $dispatcher->setEventsManager($eventManager); $dispatcher->setDefaultNamespace("Multiple\\Frontend\\Controllers\\"); return $dispatcher; }); //Registering the view component $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../apps/frontend/views/'); return $view; }); }
private static function cgiRegister() { $di = get_app_di(); $config = self::getConfig(); self::registCommonService($di, $config); $di->setShared('router', function () { $router = new \Phalcon\Mvc\Router(); $router->removeExtraSlashes(true); $router->add('/:controller/:action/:params', array('controller' => 1, 'action' => 2, 'params' => 3)); return $router; }); $di->setShared('dispatcher', function () { $eventsManager = new \Phalcon\Events\Manager(); $eventsManager->attach('dispatch:beforeException', new NotFoundPlugin()); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($eventsManager); $dispatcher->setDefaultNamespace("App\\Controllers\\"); return $dispatcher; }); $di->setShared('view', function () use($config) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir($config->view->templatePath); $view->registerEngines(array('.html' => function ($view, $di) { $config = $di->get('config'); $compiledPath = $config->view->compiledPath; if (!file_exists($compiledPath)) { mkdir($compiledPath, 0744, true); } $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array('compiledPath' => $compiledPath, 'compiledExtension' => $config->view->compiledExtension, 'compileAlways' => isset($config->view->compileAlways) ?: false)); $compiler = $volt->getCompiler(); $compiler->addExtension(new VoltExtension()); $autoEscape = isset($config->view->autoEscape) ?: true; ClassUtil::modifyPrivateProperties($compiler, array('_autoescape' => $autoEscape)); return $volt; })); return $view; }); $di->setShared('elements', function () { return new ElementsPlugin(); }); }
/** * Register specific services for the module */ public function registerServices(\Phalcon\DiInterface $dependencyInjector) { /** * Read configuration */ $config = (require_once MODULES_DIR . $this->module_name . DS . "config" . DS . "config.php"); //Registering a dispatcher $dependencyInjector->set('dispatcher', function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); //Attach a event listener to the dispatcher $eventManager = new \Phalcon\Events\Manager(); //$eventManager->attach('dispatch', new \Acl($this->module_name)); $dispatcher->setEventsManager($eventManager); $dispatcher->setDefaultNamespace("Mod\\ModMan\\Controllers\\"); return $dispatcher; }); /** * Setting up the view component */ $dependencyInjector->set('view', function () use($config) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir($config->module->viewsDir); $view->setViewsDir(MODULES_DIR . $this->module_name . DS . 'views' . DS); $view->setLayoutsDir(MODULES_DIR . $this->module_name . DS . 'layouts' . DS); //$view->setTemplateAfter('index'); $view->registerEngines(array('.volt' => function ($view, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array('compiledPath' => MODULES_DIR . $this->module_name . DS . 'views' . DS . '_compiled' . DS, 'stat' => true, 'compileAlways' => true)); return $volt; })); return $view; }); /** * Database connection is created based in the parameters defined in the configuration file * http://stackoverflow.com/questions/22197678/how-to-connect-multiple-database-in-phalcon-framework */ $dependencyInjector->set('db', function () use($config) { return new DbAdapter(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name)); }); }
public function registerServices(\Phalcon\DiInterface $di = NULL) { $config = $di->get('config'); $di->set('dispatcher', function () use($di) { $eventsManager = $di->getShared('eventsManager'); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($eventsManager); $dispatcher->setDefaultNamespace($this->namespace . '\\Controllers\\'); return $dispatcher; }); if (file_exists('../' . $config->app->apps . $this->moduleName . '/config/routers.ini')) { $router = $di->get('router'); $routers = new \Phalcon\Config\Adapter\Ini('../' . $config->app->apps . $this->moduleName . '/config/routers.ini'); if (!empty($routers)) { foreach ($routers as $name => $rule) { $rule->module = $this->moduleName; $pattern = $rule->pattern; unset($rule->pattern); $router->add($pattern, $rule->toArray())->setName($name); } } $router->handle(); } $di->set('view', function () use($config) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../' . $config->app->apps . $this->moduleName . '/' . $config->app->views); $view->registerEngines(array('.html' => function ($view, $di) use($config) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array('compiledPath' => '../' . $config->app->apps . $this->moduleName . '/cache/volt/', 'compiledExtension' => '.php', 'compileAlways' => true, 'compiledSeparator' => '_')); return $volt; })); return $view; }); $di->set('flashSession', function () { return new \Phalcon\Flash\Session(array('error' => 'alert alert-warning alert-small', 'success' => 'alert alert-success', 'notice' => 'alert alert-info')); }); $di->set('flash', function () { return new \Phalcon\Flash\Direct(array('error' => 'alert alert-error', 'success' => 'alert alert-success', 'notice' => 'alert alert-info')); }); }
/** * Register the services here to make them general or register in the ModuleDefinition to make them module-specific */ public function registerServices($di) { //Registering a dispatcher $di->set('dispatcher', function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); //Attach a event listener to the dispatcher $eventManager = new \Phalcon\Events\Manager(); $eventManager->attach('dispatch', new \Acl('frontend')); $dispatcher->setEventsManager($eventManager); $dispatcher->setDefaultNamespace("Multiple\\Frontend\\Controllers\\"); return $dispatcher; }); //Registering the view component $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../apps/frontend/views/'); return $view; }); $di->set('db', function () { return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => "localhost", "username" => "root", "password" => "secret", "dbname" => "invo")); }); }
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); }
// { // require __DIR__.'/../app/config/routes.php'; // return $router; // }); $usecdn = $config->resource->useCDN; $di->set('usecdn', function () use($usecdn) { return $usecdn; }); // setplugins if ($config->security->protectBackend) { $di->set("dispatcher", function () use($di) { $dispatcher = new Phalcon\Mvc\Dispatcher(); $eventsManager = $di->getShared("eventsManager"); $security = new Security($di); $eventsManager->attach('dispatch', $security); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); } $di->set('flash', function () { return new Phalcon\Flash\Session(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info')); }); //cookie $di->set("cookies", function () { $cookies = new Phalcon\Http\Response\Cookies(); $cookies->useEncryption(false); return $cookies; }); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise */
private function initEventManager($di) { $eventsManager = new \Phalcon\Events\Manager(); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $eventsManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher) use($di) { new \YonaCMS\Plugin\CheckPoint($di->get('request')); new \YonaCMS\Plugin\Localization($dispatcher); new \YonaCMS\Plugin\AdminLocalization($di->get('config')); new \YonaCMS\Plugin\Acl($di->get('acl'), $dispatcher, $di->get('view')); new \YonaCMS\Plugin\MobileDetect($di->get('session'), $di->get('view'), $di->get('request')); }); $eventsManager->attach("dispatch:afterDispatchLoop", function ($event, $dispatcher) use($di) { new \Seo\Plugin\SeoManager($dispatcher, $di->get('request'), $di->get('router'), $di->get('view')); new \YonaCMS\Plugin\Title($di); }); // Profiler $registry = $di->get('registry'); if ($registry->cms['PROFILER']) { $profiler = new \Phalcon\Db\Profiler(); $di->set('profiler', $profiler); $eventsManager->attach('db', function ($event, $db) use($profiler) { if ($event->getType() == 'beforeQuery') { $profiler->startProfile($db->getSQLStatement()); } if ($event->getType() == 'afterQuery') { $profiler->stopProfile(); } }); } $db = $di->get('db'); $db->setEventsManager($eventsManager); $dispatcher->setEventsManager($eventsManager); $di->set('dispatcher', $dispatcher); }
private function init_dispatcher() { $evManager = $this->_di->getShared('eventsManager'); $this->_di->set('dispatcher', function () use($evManager) { $evManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) { switch ($exception->getCode()) { case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array('controller' => 'error', 'action' => 'show404')); return false; } }); $evManager->attach("dispatch:afterExecuteRoute", function ($event, $dispatcher, $exception) { if (empty($_COOKIE[SITESLUG . '_cnu'])) { $longip = ip2long(get_ip()); $cokkie_value = $longip . '_' . get_user_id($longip); setcookie(SITESLUG . '_cnu', $cokkie_value, time() + 86400 * 360); } else { $cokkie_value = $_COOKIE[SITESLUG . '_cnu']; } //exec("wget -b -cv http://localhost:3000/collect/push/".$cokkie_value."/la10"); //exec("wget -b -cv http://127.0.0.1:8081?site=la10&cokkie_user=".$cokkie_value); return true; }); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($evManager); return $dispatcher; }, true); }
/** * Регистрация сервисов модуля */ public function registerServices(\Phalcon\DiInterface $di) { $config = new \Phalcon\Config\Adapter\Ini(__DIR__ . '/config/config.ini'); // Set site config $di->set('config', $config); // Setting up mongo $di->set('mongo', function () use($config) { $mongo = new \MongoClient($config->database->host); return $mongo->selectDb($config->database->name); }, true); // Registering the collectionManager service $di->set('collectionManager', function () { $modelsManager = new \Phalcon\Mvc\Collection\Manager(); return $modelsManager; }, true); // Start the session the first time when some component request the session service $di->set('session', function () use($config) { if (isset($config->session->adapter)) { switch ($config->session->adapter) { case 'mongo': $mongo = new \Mongo($config->session->mongoHost); $session = new \Phalcon\Session\Adapter\Mongo(array('collection' => $mongo->kladrapiSession->data)); break; case 'file': $session = new \Phalcon\Session\Adapter\Files(); break; } } else { $session = new \Phalcon\Session\Adapter\Files(); } $session->start(); return $session; }); // Setting up dispatcher $di->set('dispatcher', function () use($di) { $evManager = $di->getShared('eventsManager'); $evManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) { switch ($exception->getCode()) { case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array('controller' => 'index', 'action' => 'show404')); return false; } }); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultNamespace("Kladr\\Frontend\\Controllers"); $dispatcher->setEventsManager($evManager); return $dispatcher; }); // Register an user component $di->set('elements', function () { return new Library\Elements(); }); // Register key tools $di->set('keyTools', function () { return new Plugins\KeyTools(); }); // Register the flash service with custom CSS classes $di->set('flash', function () { $flash = new \Phalcon\Flash\Direct(); return $flash; }); // Setting up the view component $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../apps/frontend/views/'); return $view; }); }
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(); }
private function initEventManager() { $di = $this->getDi(); $eventsManager = new \Phalcon\Events\Manager(); $dispatcher = new \Phalcon\Mvc\Dispatcher(); // $dispatcher = $di->get('dispatcher'); $eventsManager->attach("dispatch", new \Plugins\CheckPoint()); $dispatcher->setEventsManager($eventsManager); $di->set('dispatcher', $dispatcher); }
/** * Encargado de escuchar cada peticion(controlador/acción) que hace el usuario a la plataforma * @return DI object */ private function setDispatcher() { $di = $this->di; $di->set('dispatcher', function () use($di) { $eventsManager = $di->getShared('eventsManager'); $security = new \Security($di); /** * We listen for events in the dispatcher using the Security plugin */ $eventsManager->attach('dispatch', $security); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); }