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"))); }; }
public function registerServices($di) { /** * Read configuration */ $config = (include __DIR__ . "/config/config.php"); $di['dispatcher'] = function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultNamespace("Modules\\Frontend\\Controllers"); return $dispatcher; }; /** * Setting up the view component */ $di['view'] = function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/views/'); $view->setLayoutsDir('../../common/layouts/'); $view->setTemplateAfter('main'); 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)); }; }
public static function initView() { $config = \Phalcon\DI::getDefault()->get('config'); $view = new \Phalcon\Mvc\View(); $view->setTemplateAfter('main'); // for escaping in views $view->setVar('escaper', new \Phalcon\Escaper()); $view->setViewsDir($config->view->dir); return $view; }
protected function _registerServices() { $loader = $this->loader; $dirs = $this->loader->getConfigDirs('../'); $this->di->set(Service::LOADER, function () use($loader) { return $loader; }, true); $theme = 'default/'; //$theme = 'javj/' ; $this->di->set(Service::VIEW, function () use($dirs, $theme) { $view = new \Phalcon\Mvc\View(); $view->setLayoutsDir('../../../' . $dirs->ui->themes . $theme); $view->setPartialsDir('../../../' . $dirs->ui->themes . $theme . 'partials/'); $view->setTemplateAfter('main'); $view->hook = new Hook(); //TODO manage themes if (is_dir($dirs->ui->themes . $theme . 'assets/')) { rcopy($dirs->ui->themes . $theme . 'assets/', $dirs->assets->themes . $theme, true); } return $view; }, true); $this->di->set(Service::THEME_NAME, function () use($theme) { return str_replace('/', '', $theme); }, true); $this->di->set(Service::URL, function () use($dirs) { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri($dirs->base->uri); return $url; }, true); $this->di->set(Service::ROUTER, function () { $router = new Router(false); $router->add('core/ui/themes/default/', array('controller' => 'index'))->setName('theme'); return $router; }, true); $this->di->set(Service::DISPATCHER, function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); return $dispatcher; }, true); $this->di->set(Service::VOLT, function ($view, $di) use($dirs) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array("compiledPath" => $dirs->cache->volt)); return $volt; }, true); $this->di->set(Service::DB, function () { return $this->loader->getDbConnection(self::$_token); }); }
/** * Generate a new View object with preset parameters. * * @param array $options * @param \Phalcon\DiInterface $di * @return \Phalcon\Mvc\View */ public static function getView($options = array(), \Phalcon\DiInterface $di = null) { if ($di == null) { $di = \Phalcon\Di::getDefault(); } $defaults = array('base_dir' => FA_INCLUDE_BASE . '/', 'views_dir' => 'modules/frontend/views/scripts/', 'partials_dir' => '', 'layouts_dir' => '../../../../templates/', 'layout' => 'main'); $options = array_merge($defaults, (array) $options); // Temporary fix to force "views_dir" to be the full path, because "base_dir" is not used in some Phalcon calculations. $options['views_dir'] = $options['base_dir'] . $options['views_dir']; $options['base_dir'] = ''; $view = new \Phalcon\Mvc\View(); $view->setDI($di); $eventsManager = new \Phalcon\Events\Manager(); $view->setEventsManager($eventsManager); // Base directory from which all views load. $view->setBasePath($options['base_dir']); $view->setViewsDir($options['views_dir']); // Relative path of main templates. $view->setLayoutsDir($options['layouts_dir']); $view->setTemplateAfter($options['layout']); // Use present directory for partials by default. $view->setPartialsDir($options['partials_dir']); // Register template engines. $view->registerEngines(array(".phtml" => 'Phalcon\\Mvc\\View\\Engine\\Php', ".volt" => function ($view, $di) { $volt = new Volt($view, $di); $volt->setOptions(array('compileAlways' => FA_APPLICATION_ENV == 'development', 'compiledPath' => function ($templatePath) { // Clean up the template path and remove non-application folders from path. $templatePath = realpath($templatePath); $templatePath = ltrim(str_replace(FA_INCLUDE_BASE, '', $templatePath), '/'); $find_replace = array('/views/scripts/' => '_', '../' => '', '/' => '_', '.volt' => ''); $templatePath = str_replace(array_keys($find_replace), array_values($find_replace), $templatePath); return FA_INCLUDE_CACHE . '/volt_' . $templatePath . '.compiled.php'; })); $compiler = $volt->getCompiler(); $compiler->addExtension(new \FA\Phalcon\Service\ViewHelper()); return $volt; })); return $view; }
/** * Generate a new View object with preset parameters. * * @param array $options * @param \Phalcon\DiInterface $di * @return \Phalcon\Mvc\View */ public static function getView($options = array(), \Phalcon\DiInterface $di = null) { if ($di == null) { $di = \Phalcon\Di::getDefault(); } $defaults = array('base_dir' => DF_INCLUDE_BASE . '/', 'views_dir' => 'modules/frontend/views/scripts/', 'partials_dir' => '', 'layouts_dir' => '../../../../templates/', 'layout' => 'main'); $options = array_merge($defaults, (array) $options); $view = new \Phalcon\Mvc\View(); $view->setDI($di); $eventsManager = new \Phalcon\Events\Manager(); $view->setEventsManager($eventsManager); // Base directory from which all views load. $view->setBasePath($options['base_dir']); $view->setViewsDir($options['views_dir']); // Relative path of main templates. $view->setLayoutsDir($options['layouts_dir']); $view->setTemplateAfter($options['layout']); // Use present directory for partials by default. $view->setPartialsDir($options['partials_dir']); // Register template engines. $view->registerEngines(array(".phtml" => 'Phalcon\\Mvc\\View\\Engine\\Php', ".volt" => function ($view, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array('compiledPath' => function ($templatePath) { $find_replace = array(DF_INCLUDE_BASE => '', '/modules/' => '', '/views/scripts/' => '_', '/' => '_'); $templatePath = str_replace(array_keys($find_replace), array_values($find_replace), $templatePath); return DF_INCLUDE_CACHE . '/volt_' . $templatePath . '.compiled.php'; })); $compiler = $volt->getCompiler(); $compiler->addFunction('helper', function ($resolvedArgs, $exprArgs) use($di) { return '$this->viewHelper->handle(' . $resolvedArgs . ')'; }); return $volt; })); // Register global escaper. $view->setVar('e', new \Phalcon\Escaper()); return $view; }
//Create a DI $di = new Phalcon\DI\FactoryDefault(); //Register Volt as a service $di->set('volt', function ($view, $di) { $volt = new Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array("compileAlways" => true)); return $volt; }); //Register Volt as template engine $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); //hasilnya folder layout = "../app/views/templates/"; $view->setLayoutsDir('layouts/'); //hasilnya = "../app/views/templates/main"; $view->setTemplateAfter('main'); $view->registerEngines(array(".volt" => 'volt')); return $view; }); //Setup a base URI so that all generated URIs include the "tutorial" folder $di->set('url', function () { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/Ovs/'); //$url->setStaticBaseUri('http://www.ovs.my.id/'); $url->setStaticBaseUri('http://localhost/ovs'); return $url; }); //Handle the request $application = new \Phalcon\Mvc\Application($di); echo $application->handle()->getContent(); } catch (\Phalcon\Exception $e) {