/** * Register specific services for the module * @param \Phalcon\DiInterface $di */ public function registerServices(DiInterface $di) { $config = $this->_config; //Registering a dispatcher $di->set('dispatcher', function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('Imports'); return $dispatcher; }); //Registering the view component $vDI = $di; $di->set('volt', function ($view, $vDI) use($config) { $volt = new Volt($view, $vDI); $volt->setOptions(array('compiledPath' => APP_PATH . $config->volt->path, 'compiledExtension' => $config->volt->extension, 'compiledSeparator' => $config->volt->separator, 'stat' => (bool) $config->volt->stat)); $compiler = $volt->getCompiler(); //Add funcao $compiler->addFunction('is_a', 'is_a'); return $volt; }); /** * Configura o serviço de view */ $di->set('view', function () use($config, $vDI) { $view = new View(); $view->setViewsDir(APP_PATH . $config->application->viewsDir); $view->registerEngines(array('.volt' => 'volt')); return $view; }); return $di; }
/** * Registers the module-only services * * @param Phalcon\DI $di */ public function registerServices(\Phalcon\DiInterface $di) { /** * Read configuration */ /** * Setting up the view component */ $config = $this->config; $di->set('view', function () use($config) { $view = new View(); $view->setViewsDir($config->application->backendViewsDir); $view->registerEngines(array(".volt" => 'volt')); return $view; }, true); /** * Setting up volt */ $di->set('volt', function ($view, $di) use($config) { $volt = new Volt($view, $di); $volt->setOptions(array("compiledPath" => APP_PATH . "/app/cache/volt/", "compiledSeparator" => "_", "compileAlways" => $config->application->debug)); $volt->getCompiler()->addFunction('tr', function ($key) { return "messetool\\Modules\\Modules\\Backend\\Controllers\\ControllerBase::translate({$key})"; }); $volt->getCompiler()->addFunction('number_format', function ($resolvedArgs) { return 'number_format(' . $resolvedArgs . ')'; }); $volt->getCompiler()->addFunction('linkAllowed', function ($args) { return "messetool\\Acl\\Acl::linkAllowed({$args})"; }); return $volt; }, true); }
/** * {@inheritdoc} * @see http://docs.phalconphp.com/pl/latest/reference/odm.html */ public function register(DiInterface $di) { $di->set(self::SERVICE_NAME, function () use($di) { $mongoConfig = $di->get('config')->mongo->toArray(); if (isset($mongoConfig['dsn'])) { $hostname = $mongoConfig['dsn']; unset($mongoConfig['dsn']); } else { //obtains hostname if (isset($mongoConfig['host'])) { $hostname = 'mongodb://' . $mongoConfig['host']; } else { $hostname = 'mongodb://localhost'; } if (isset($mongoConfig['port'])) { $hostname .= ':' . $mongoConfig['port']; } //removes options that are not allowed in MongoClient constructor unset($mongoConfig['host']); unset($mongoConfig['port']); } $dbName = $mongoConfig['dbname']; unset($mongoConfig['dbname']); $mongo = new \MongoClient($hostname, $mongoConfig); return $mongo->selectDb($dbName); }, true); }
public static function defineRoutes(DiInterface $di) { /* @var $router \Phalcon\Mvc\Router */ $router = $di->get('router'); $default = $router->getDefaults()['module']; $router->add("/customroute/foo/:params", array('module' => self::$modname, 'controller' => 'index', 'action' => 'foo', 'params' => 1))->setName(self::$modname . '_foo'); }
/** * @param string $name * @return mixed * @throws \Exception */ public function get($name) { if ($this->di->has($name)) { return $this->di->get($name); } throw new \Exception("Service \"{$name}\" is not defined"); }
/** * Registers the module-only services * * @param Phalcon\DI $di */ public function registerServices(\Phalcon\DiInterface $di = NULL) { /** * Read configuration */ $config = (include __DIR__ . "/config/config.php"); /** * Setting up the view component */ $di['view'] = function () { $view = new View(); $view->setViewsDir(__DIR__ . '/views/'); return $view; }; /** * Database connection is created based in the parameters defined in the configuration file */ $di['db'] = function () use($config) { return new Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "options" => array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'", \PDO::ATTR_CASE => \PDO::CASE_LOWER, \PDO::ATTR_EMULATE_PREPARES => false))); }; $di->set('dispatcher', function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultNamespace("Mall\\Admin\\Controllers"); return $dispatcher; }); $di->set('cookies', function () { $cookies = new \Phalcon\Http\Response\Cookies(); $cookies->useEncryption(false); return $cookies; }); }
/** * Register specific services for the module */ public function registerServices(DiInterface $di) { // Assign our new tag a definition so we can call it $di->set('Utilitarios', function () { return new \Ecommerce\Admin\Helpers\UtilitariosHelper(); }); $di->set('dispatcher', function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('Ecommerce\\Admin\\Controllers'); return $dispatcher; }); // Registering the view component $di->set('view', function () { $config = (include __DIR__ . "/config/config.php"); $view = new View(); $view->registerEngines(array('.volt' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_')); return $volt; }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php')); $view->setViewsDir('../apps/admin/views/'); return $view; }); include "../apps/admin/vendor/autoload.php"; }
/** * {@inheritdoc} */ public function register(DiInterface $di) { $di->set(self::SERVICE_NAME, function () use($di) { $session = new Vegas\Session($di->get('session')); return $session; }, true); }
/** * @param \Phalcon\DiInterface $di * * @return \Phalcon\Cli\Console */ protected function getConsole(\Phalcon\DiInterface $di) { if (!$di->has("console")) { return $this->getDefaultConsole($di); } return $di->get("console"); }
/** * {@inheritdoc} */ public function register(DiInterface $di) { $di->set(self::SERVICE_NAME, function () use($di) { $mongo = new \MongoClient(); return $mongo->selectDb($di->get('config')->mongo->db); }, true); }
/** * Register specific services for the module */ function registerServices(\Phalcon\DiInterface $di) { //Registering a dispatcher $di->set('dispatcher', function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('Modules\\Frontend\\Controllers'); return $dispatcher; }); $config = $di->getShared('config'); $di->set('view', function () use($config, $di) { $view = new View(); $router = $di->getShared('router'); /* * @todo 给layouts等目录统一变量 * */ $view->setViewsDir(__DIR__ . '/views/'); $view->setLayoutsDir('/../../../layouts/'); // 多模块的话, 可能会有多个风格,所以需要切换layout,这里的Path是根据当前module的Path向上走的 $view->setLayout('index'); $view->registerEngines(array('.volt' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_')); return $volt; }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $view; }, true); }
/** * @param \Phalcon\DiInterface $di * * @return \Phalcon\Mvc\Application */ protected function getApplication(\Phalcon\DiInterface $di) { if (!$di->has("application")) { return $this->getDefaultApplication($di); } return $di->get("application"); }
public function setDI(DiInterface $di) { $di->setShared('config', static::$config); $this->initEventsManager($di); parent::setDI($di); Di::setDefault($di); }
/** * 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; }); }
public function registerServices(\Phalcon\DiInterface $di = null) { /** * Read configuration */ $config = (include dirname(dirname(dirname(__DIR__))) . "/apps/config/config.php"); $di->set('dispatcher', function () use($di) { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('Modules\\Frontend\\Controllers'); return $dispatcher; }, true); $di->set('view', function () use($config) { $view = new View(); $view->setViewsDir(__DIR__ . '/views/'); $view->registerEngines(array('.volt' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_')); return $volt; }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $view; }); /** * Database connection is created based in the parameters defined in the configuration file */ $di['db'] = function () use($config) { return new DbAdapter(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, 'charset' => 'utf8')); }; }
/** * 注册服务 * @param type $di */ public function registerServices(\Phalcon\DiInterface $di = NULL) { $config = \TConfig::instance()->getModule($this->moduleId); if (php_sapi_name() == 'cli') { //处理CLI模式 $di['dispatcher'] = function () use($config) { $dispatcher = new \Phalcon\Cli\Dispatcher(); if (isset($config['defaultNamespace']) && isset($config['defaultNamespace']['task'])) { $dispatcher->setDefaultNamespace($config['defaultNamespace']); } return $dispatcher; }; } else { //处理WEB模式 $di['dispatcher'] = function () use($config) { $dispatcher = new \Phalcon\Mvc\Dispatcher(); if (isset($config['defaultNamespace']) && isset($config['defaultNamespace']['controller'])) { $dispatcher->setDefaultNamespace($config['defaultNamespace']['controller']); } return $dispatcher; }; //添加视图 $di->set('view', function () use($config) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir($config['autoloadDir']['view']); $view->registerEngines(array('.html' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $view; }); } }
/** * {@inheritdoc} * @see http://docs.phalconphp.com/en/latest/api/Phalcon_Db_Adapter_Pdo_Postgresql.html */ public function register(DiInterface $di) { $di->set(self::SERVICE_NAME, function () use($di) { $config = $di->get('config'); return new Phalcon\Db\Adapter\Pdo\Postgresql(["host" => $config->db->host, "dbname" => $config->db->dbname, "port" => $config->db->port, "username" => $config->db->username, "password" => $config->db->password]); }, true); }
public function registerServices(DiInterface $di) { global $config; $di->setShared('url', function () use($config) { $url = new UrlResolver(); $url->setBaseUri($config->backend->baseUri); return $url; }); $di->setShared('dispatcher', function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace("Multiple\\Backend\\Controllers"); return $dispatcher; }); $di->setShared('view', function () use($config) { $view = new View(); $view->setViewsDir($config->backend->viewsDir); $view->setLayoutsDir('layouts/'); $view->setPartialsDir('partials/'); $view->registerEngines(array('.phtml' => function ($view, $di) use($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array('compiledPath' => $config->backend->cacheDir, 'compiledSeparator' => '_')); return $volt; }, '.volt' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $view; }); }
/** * {@inheritdoc} */ public function register(DiInterface $di) { $di->set(self::SERVICE_NAME, function () use($di) { $arrayConfig = $di->get('config')->db->toArray(); return new \Phalcon\Db\Adapter\Pdo\Mysql($arrayConfig); }, true); }
/** * Registers services related to the module * * @param DiInterface $di Dependency Injection Container */ public function registerServices(DiInterface $di) { /** * Read configuration */ $config = (require __DIR__ . "/config/config.php"); /** * Setting up the view component */ $di->setShared('view', function () { $view = new View(); $view->setViewsDir(__DIR__ . '/views/'); $view->setTemplateBefore('main'); $view->registerEngines([".volt" => function ($view, $di) { $volt = new Volt($view, $di); $volt->setOptions(['compiledPath' => function ($templatePath) { return realpath(__DIR__ . "/../../var/volt") . '/' . md5($templatePath) . '.php'; }, 'compiledExtension' => '.php', 'compiledSeparator' => '%']); return $volt; }]); return $view; }); /** * Database connection is created based in the parameters defined in the configuration file */ $di->setShared('db', function () use($config) { return new Connection(['host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname]); }); }
/** * Registers services related to the module * * @param DiInterface $di */ public function registerServices(DiInterface $di) { /** * 获取全局配置 */ $config = $di->has('config') ? $di->getShared('config') : null; /** * 各模块可自定义config文件并替换全局的config配置 */ if (file_exists($this->modulePath . '/config/config.php')) { $override = new Config(include $this->modulePath . '/config/config.php'); if ($config instanceof Config) { $config->merge($override); } else { $config = $override; } } //重置全局配置 $di->setShared('config', $config); /** * 设置各个模块的视图目录 */ $view = new View(); //$view->setViewsDir($this->modulePath . '/views/default/'); $view->setViewsDir(FRONTEND_PUBLIC_PATH . 'views/default/'); $view->registerEngines(['.volt' => 'volt', '.php' => 'volt', '.html' => 'volt']); $di->set('view', $view); }
/** * {@inheritdoc} */ public function register(DiInterface $di) { $di->set(self::SERVICE_NAME, function () use($di) { $config = $di->get('config'); return new Phalcon\Db\Adapter\Pdo\Mysql(["host" => $config->database->host, "dbname" => $config->database->dbname, "port" => $config->database->port, "username" => $config->database->username, "password" => $config->database->password, "options" => [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']]); }, true); }
/** * {@inheritdoc} * @see http://docs.phalconphp.com/en/latest/api/Phalcon_Db_Adapter_Pdo_Sqlite.html */ public function register(DiInterface $di) { $di->set(self::SERVICE_NAME, function () use($di) { $config = $di->get('config'); return new Phalcon\Db\Adapter\Pdo\Sqlite(array("dbname" => $config->db->dbname)); }, true); }
/** * {@inheritdoc} */ public function register(DiInterface $di) { $config = $di->get('config'); $di->set(self::SERVICE_NAME, function () use($config) { return new \Vegas\Translate\Adapter\Gettext(['locale' => $config->application->language, 'file' => 'messages', 'directory' => APP_ROOT . '/lang']); }); }
/** * {@inheritdoc} * * @param \Phalcon\DI $di */ public function registerServices(DiInterface $di = null) { $di->getDispatcher()->setDefaultNamespace('Webird\\Web\\Controllers'); $di->setShared('view', $this->getViewFunc($di)); if (DEV_ENV === ENV) { $debugPanel = new DebugPanel($di); } // //Listen for events produced in the dispatcher using the Security plugin // $evManager = $di->getShared('eventsManager'); // // $evManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) use ($di) { // // switch ($exception->getCode()) { // // case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: // case Dispatcher::EXCEPTION_ACTION_NOT_FOUND: // // $dispatcher->forward([ // 'controller' => 'errors', // 'action' => 'show404', // ]); // // return FALSE; // break; // } // }); }
/** * Register the services here to make them general * or register in the ModuleDefinition to make them module-specific */ public function registerServices(DiInterface $di) { //Read configuration $config = (include __DIR__ . "/config/config.php"); // The URL component is used to generate all kind of urls in the application $di->set('url', function () use($config) { $url = new Url(); $url->setBaseUri($config->application->baseUri); return $url; }); //Registering a dispatcher $di->set('dispatcher', function () { //Create/Get an EventManager $eventsManager = new EventsManager(); //Attach a listener $eventsManager->attach('dispatch', function ($event, $dispatcher, $exception) { //controller or action doesn't exist if ($event->getType() == 'beforeException') { switch ($exception->getCode()) { case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(['module' => 'backend', 'controller' => 'errors', 'action' => 'notFound']); return false; } } }); $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace("Phanbook\\Backend\\Controllers"); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); /** * Setting up the view component */ $di->set('view', function () use($config) { $view = new View(); $view->setViewsDir($config->application->viewsDir); $view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]); $view->registerEngines(['.volt' => 'volt']); // Create an event manager $eventsManager = new EventsManager(); // Attach a listener for type 'view' $eventsManager->attach('view', function ($event, $view) { if ($event->getType() == 'notFoundView') { throw new \Exception('View not found!!! (' . $view->getActiveRenderPath() . ')'); } }); // Bind the eventsManager to the view component $view->setEventsManager($eventsManager); return $view; }); $configMenu = (include __DIR__ . "/config/config.menu.php"); $di->setShared('menuStruct', function () use($configMenu) { // if structure received from db table instead getting from $config // we need to store it to cache for reducing db connections $struct = $configMenu->get('menuStruct')->toArray(); return $struct; }); }
/** * @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)); }; }
protected function registerConfig() { $config = (require __DIR__ . '/config/config.php'); if ($this->di->has('console.config')) { $config->merge($this->di->get('console.config')); } $this->di['console.config'] = $config; }
/** * {@inheritdoc} */ public function register(DiInterface $di) { $di->set(self::SERVICE_NAME, function () use($di) { $url = new UrlResolver(); $url->setBaseUri($di->get('config')->application->baseUri); return $url; }, true); }
public function registerServices(DiInterface $di) { $di->set('view', function () { $view = new View(); $view->setViewsDir(PATH_DATA . 'modules/backend/views/'); return $view; }); }