/** * 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 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; }); } }
protected function setView() { $view = new \Phalcon\Mvc\View(); $view->setViewsDir($this->dir . '/../views/'); $view->setLayoutsDir('layouts/'); $view->setLayout('main'); $view->registerEngines(['.volt' => $this->setVolt($view), '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php']); return $view; }
/** * * 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; }); }
public function setView() { $this->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); $view->registerEngines(['.vhtml' => 'voltService']); //$view->setLayoutsDir('../views/'); $view->setPartialsDir('partials/'); $view->setMainView('main'); return $view; }); }
protected function registerView() { $this->di->set('volt.service', function ($view, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array("compiledPath" => ROOT_PATH . "/public/views/", "compiledExtension" => ".compiled", 'compiledSeparator' => '_', 'stat' => true, 'compileAlways' => true)); return $volt; }); $this->di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(APP_PATH . '/views/'); $view->registerEngines(array(".phtml" => 'volt.service')); return $view; }); }
public function registerServices($di) { $di['view'] = function () { $view = new \Phalcon\Mvc\View(); $view->registerEngines(array('.html' => function ($view, $di) { $volt = new VoltEngine($view, $di); //$volt->setOptions(array( //'compiledPath' => $di->get('config')["common"]["application"]['cacheDir'], //'compiledSeparator' => '_' //)); return $volt; })); $view->setViewsDir(__DIR__ . '/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(); }); }
/** * Display firewall code * * @package las * @version 1.0 */ public static function display($path, $vars = []) { $di = \Phalcon\DI::getDefault(); if ($di->getShared('router')->getModuleName() == 'cli') { $view = $di->getShared('view'); } else { $view = new \Phalcon\Mvc\View(); $view->setDI($di); $view->registerEngines(\Las\Library\Tool::registerEngines($view, $di)); } $settings = json_decode(json_encode(\Las\Library\Arr::from_model(Settings::find('status=' . Settings::ACTIVE), 'name', 'value'))); $lans = Networks::find('status = ' . Networks::ACTIVE . ' AND type = ' . Networks::LAN); $wans = Networks::find('status = ' . Networks::ACTIVE . ' AND type = ' . Networks::WAN); $vars = ['clients' => Clients::find(), 'devices' => Devices::find('status=' . Devices::ACTIVE), 'messages' => Messages::find('status=' . Messages::ACTIVE), 'tariffs' => Tariffs::find('status=' . Tariffs::ACTIVE), 'settings' => $settings, 'redirects' => Redirects::find('status=' . Redirects::ACTIVE), 'services' => Services::find('status=' . Services::ACTIVE . ' AND client_id=0 AND device_id=0'), 'lans' => $lans, 'lan' => $lans->getFirst(), 'wans' => $wans, 'wan' => $wans->getFirst(), 'ipt' => $settings->iptables, 'tc' => $settings->tc, 'EOL' => PHP_EOL]; $view->setViewsDir(ROOT_PATH . '/app/common/cache/volt/app/cli/views/'); ob_start(); $view->partial($path, $vars); return preg_replace(['/^\\s+|^[\\t\\s]*\\n+/m', "/\r/"], '', ob_get_clean()); //return preg_replace('/^\s+|^[\t\s]*\n+/m', "/\x0D/"], '', $view->partial($path, $vars, false)); }
/** * 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('backend')); $dispatcher->setEventsManager($eventManager); $dispatcher->setDefaultNamespace("Multiple\\Backend\\Controllers\\"); return $dispatcher; }); //Registering the view component $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../apps/backend/views/'); $view->registerEngines(array(".phtml" => 'volt')); return $view; }); }
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')); }); }
/** * Initializes Volt engine */ public function register() { $di = $this->getDi(); $eventsManager = $this->getEventsManager(); $config = $this->_config; $moduleDirectory = $this->_module->getModuleDirectory(); $defaultModuleDir = $this->_module->getDefaultModuleDirectory(); $di->set('view', function () use($di, $moduleDirectory, $defaultModuleDir, $eventsManager, $config) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir($moduleDirectory . '/View/'); $view->setLayoutsDir($defaultModuleDir . '/View/layouts/'); $view->registerEngines([".volt" => 'viewEngine']); // Attach a listener for type "view" if (!$config->application->debug) { $eventsManager->attach("view", function ($event, $view) use($di) { if ($event->getType() == 'notFoundView') { $di->get('logger')->error('View not found - "' . $view->getActiveRenderPath() . '"'); } }); $view->setEventsManager($eventsManager); } elseif ($config->application->profiler) { $eventsManager->attach("view", function ($event, $view) use($di) { if ($di->has('profiler')) { if ($event->getType() == 'beforeRender') { $di->get('profiler')->start(); } if ($event->getType() == 'afterRender') { $di->get('profiler')->stop($view->getActiveRenderPath(), 'view'); } } if ($event->getType() == 'notFoundView') { $di->get('logger')->error('View not found - "' . $view->getActiveRenderPath() . '"'); } }); $view->setEventsManager($eventsManager); } 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' => 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; }
<?php try { $config = new Phalcon\Config\Adapter\Ini('../app/config/configuration.ini'); //Register an autoloader $loader = new \Phalcon\Loader(); $loader->registerDirs(array('../app/controllers/', '../app/models/', '../app/forms/', '../app/views/', '../app/plugins/', '../app/wrappers/')); $loader->registerNamespaces(array('Silar\\Misc' => '../app/misc/'), true); $loader->register(); //Create a DI $di = new Phalcon\DI\FactoryDefault(); //Registering Volt as template engine $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); $view->registerEngines(array(".volt" => 'Phalcon\\Mvc\\View\\Engine\\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('/silar/'); return $url; }); $di->setShared('session', function () { $session = new Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); $di->set('router', function () { $router = new \Phalcon\Mvc\Router\Annotations();
$di->set('db', function () use($config) { return new \Phalcon\Db\Adapter\Pdo\Postgresql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name)); }); $di->set('session', function () { $session = new Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); $di->set('dispatcher', function () use($di) { $eventsManager = $di->getShared('eventsManager'); $security = new Security($di); $eventsManager->attach('dispatch', $security); $dispatcher = new Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); $view->registerEngines(array(".volt" => function ($view, $id) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $id); $volt->setOptions(array('compileAlways' => true)); return $volt; })); return $view; }); $application = new \Phalcon\Mvc\Application($di); echo $application->handle()->getContent(); } catch (\Phalcon\Exception $e) { echo "PhalconException: ", $e->getMessage(); }
public function testVoltEngineBuiltInFunctions() { @unlink('unit-tests/views/test11/index.volt.php'); $di = new Phalcon\DI(); $view = new Phalcon\Mvc\View(); $view->setDI($di); $view->setViewsDir('unit-tests/views/'); $view->registerEngines(array('.volt' => 'Phalcon\\Mvc\\View\\Engine\\Volt')); $view->setVar('arr', array(1, 2, 3, 4)); $view->setVar('obj', new SomeObject(array(1, 2, 3, 4))); $view->setVar('str', 'hello'); $view->setVar('no_str', 1234); $view->start(); $view->render('test11', 'index'); $view->finish(); $this->assertEquals($view->getContent(), 'Length Array: 4Length Object: 4Length String: 5Length No String: 4Slice Array: 1,2,3,4Slice Array: 2,3Slice Array: 1,2,3Slice Object: 2,3,4Slice Object: 2,3Slice Object: 1,2Slice String: helSlice String: elSlice String: lloSlice No String: 123Slice No String: 23Slice No String: 34'); }
return $url; }); //////////////////////////// // ROUTER $router->setDefaults(array('controller' => 'index', 'action' => 'index', 'namespace' => 'Host\\Controller')); $router->add("/extender", array('controller' => 'extender', 'action' => 'index', 'path' => $_GET['path'], 'namespace' => 'Host\\Controller')); $router->add("/extender/extend", array('controller' => 'extender', 'action' => 'index', 'path' => $_GET['path'], 'namespace' => 'Host\\Controller')); $router->notFound(array('controller' => 'index', 'action' => 'notFound', 'namespace' => 'Host\\Controller')); //////////////////////////// // BIND VIEW Twig_Autoloader::register(); $di['twigService'] = function ($view, $di) { Phalcon\Mvc\View\Engine\Twig::setOptions(array('debug' => true, 'charset' => 'UTF-8', 'base_template_class' => 'Twig_Template', 'strict_variables' => false, 'autoescape' => false, 'cache' => __DIR__ . '/../../cache/twig/', 'auto_reload' => null, 'optimizations' => -1)); $twig = new View($view, $di); return $twig; }; $di->set('twigService', function ($view, $di) use($config) { // create twig object $option = array('cache' => '../cache/'); $arrFolder = array('../views/'); $twig = new \Phalcon\Mvc\View\Engine\Twig($view, $di, $options, $arrFolder); return $twig; }, true); $di->set('view', function () { // create view $view = new \Phalcon\Mvc\View(); // set directories $view->setViewsDir('../views/'); $view->registerEngines(array('.twig' => 'twigService')); return $view; });
$di->set('router', require APP_PATH . '/app/config/routes.php'); //MetaData $di->set('modelsMetadata', function () { if (function_exists('apc_store')) { return new Phalcon\Mvc\Model\MetaData\Apc(); } else { return new Phalcon\Mvc\Model\MetaData\Memory(array('metaDataDir' => APP_PATH . "/app/compiled-templates/")); } }); // Setting up the view component (seems to be required even when not used) $di->set('view', function () use($config) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir($config->application->viewsDir); $view->registerEngines(array(".volt" => function ($view, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array("compiledPath" => APP_PATH . "/app/compiled-templates/", "compiledExtension" => ".compiled", "compiledSeparator" => '_')); return $volt; })); return $view; }); // Setting up the database connection $di->set('db', function () use($config) { $database = $config->database; return new \Phalcon\Db\Adapter\Pdo\Mysql(array('host' => $database->host, 'username' => $database->username, 'password' => $database->password, 'dbname' => $database->name, 'options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'))); }); // Setting up the mongodb connection $di->set('mongo', function () use($config) { $mongodbConfig = $config->mongodb; $mongo = new \MongoClient($mongodbConfig->url); return $mongo->{$mongodbConfig->db}; });
private function initView($di) { $view = new \Phalcon\Mvc\View(); define('MAIN_VIEW_PATH', '../../../views/'); $view->setMainView(MAIN_VIEW_PATH . 'main'); $view->setLayoutsDir(MAIN_VIEW_PATH . '/layouts/'); $view->setLayout('main'); $view->setPartialsDir(MAIN_VIEW_PATH . '/partials/'); // Volt $volt = new \Application\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(['compiledPath' => APPLICATION_PATH . '/../data/cache/volt/']); $volt->initCompiler(); $phtml = new \Phalcon\Mvc\View\Engine\Php($view, $di); $viewEngines = [".volt" => $volt, ".phtml" => $phtml]; $view->registerEngines($viewEngines); $ajax = $di->get('request')->getQuery('_ajax'); if ($ajax) { $view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_LAYOUT); } $di->set('view', $view); return $view; }
<?php $di['dispatcher'] = function () use($di) { $eventsManager = $di->getShared('eventsManager'); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($eventsManager); $dispatcher->setDefaultNamespace('App\\Frontend\\Controllers'); return $dispatcher; }; $di['url']->setBaseUri('' . $config->application->baseUri . ''); $di['view'] = function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/../Views/Default/'); //Use the global voltSevice we defined in config/services.php $view->registerEngines(array('.volt' => 'voltService')); return $view; };
try { //Autoloader $loader = new \Phalcon\Loader(); $loader->registerDirs(['../app/controllers/', '../app/models/', '../app/config/']); $loader->register(); //Dependency Injection $di = new \Phalcon\DI\FactoryDefault(); $di->set('db', function () { $db = new \Phalcon\Db\Adapter\Pdo\Mysql(["host" => "localhost", "username" => "phalcon", "password" => "TvevzMjmJmE7EHr4", "dbname" => "learning_phalcon"]); return $db; }); $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views'); $view->registerEngines([".volt" => "Phalcon\\Mvc\\View\\Engine\\Volt"]); return $view; }); $di->set('router', function () { $router = new \Phalcon\Mvc\Router(); $router->mount(new Routes()); //Call it whatever name your php file is return $router; }); //Session start $di->setShared('session', function () { $session = new \Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); //Flash data (Temporary data)
$dispatcher = new Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () use($config) { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri($config->application->baseUri); return $url; }); $di->set('view', function () use($config) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . $config->application->viewsDir); $view->registerEngines(array(".volt" => 'volt')); return $view; }); $di->set('viewCache', function () { // Кэширование данных на сутки по умолчанию $frontCache = new Phalcon\Cache\Frontend\Output(array("lifetime" => 86400)); // Настройки соединения с Memcached $cache = new Phalcon\Cache\Backend\Apc($frontCache, array()); return $cache; }); $di->set('volt', function ($view, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array("compiledPath" => "../cache/volt/")); return $volt; }, true); $di->set('db', function () use($config) {
$di->set('cache', function () use($conf) { $frontCache = new \Phalcon\Cache\Frontend\Data(array("lifetime" => $conf->cache->lifetime)); $cache = false; $cache = new \Rpp\Extend\Memcache($frontCache, array("host" => $conf->cache->memcache->host, "port" => $conf->cache->memcache->port, "prefix" => $conf->cache->memcache->prefix, "statsKey" => $conf->cache->memcache->stats)); $cache->localconnect(); return $cache; }); $view = new \Phalcon\Mvc\View(); $view->setViewsDir(APPFULLPATH . $conf->phalcon->viewsDir); $view->registerEngines(array($conf->volt->extn => function ($view, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $compiler = $volt->getCompiler(); $compiler->addFunction('getNode', '\\Rpp\\Services\\Get\\Content::node'); $compiler->addFunction('getPart', '\\Rpp\\Services\\Get\\Content::part'); $compiler->addFunction('nUrl', '\\Rpp\\Services\\Get\\Content::nurl'); $compiler->addFunction('TrackUrl', '\\Rpp\\Services\\Get\\UrlTrack::add_params'); $compiler->addFunction('TrackUrlSocial', '\\Rpp\\Services\\Get\\UrlTrack::add_params_social'); $compiler->addFunction('cdn_image', '\\Rpp\\Services\\Get\\UrlMedia::image'); $compiler->addFunction('df_match', '\\Rpp\\Services\\Get\\Resultados::match'); $compiler->addFunction('df_campeonato', '\\Rpp\\Services\\Get\\Resultados::campeonato'); $compiler->addFunction('df_torneo', '\\Rpp\\Services\\Get\\Dfactory::torneo'); return $volt; })); $view->setVar("trackurl", true); $view->setVar("host", $conf->server->host); $view->setVar("eversion", $conf->estaticos->version); $view->setVar("ehost", $conf->estaticos->host); $di->set('view', $view); $di->set('viewCache', function () use($conf) { $frontCache = new Phalcon\Cache\Frontend\Output(array("lifetime" => $conf->viewcache->lifetime)); /*$cache = new Rpp\Extend\Publish\MemcacheView($frontCache, array( "host" => $conf->viewcache->memcache->host,
// $config = new Phalcon\Config\Adapter\Ini('../app/config/config.ini'); $config = (include __DIR__ . "/../app/config/config.php"); //Register an autoloader $loader = new \Phalcon\Loader(); $loader->registerDirs(array($config->application->controllersDir, $config->application->pluginsDir, $config->application->libraryDir, $config->application->modelsDir))->register(); //Create a DI $di = new Phalcon\DI\FactoryDefault(); //Setup the database service $di->set('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')); }); //Setup the view component $di->set('view', function () use($config) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir($config->application->viewsDir); $view->registerEngines(array(".phtml" => 'Phalcon\\Mvc\\View\\Engine\\Php', ".volt" => 'volt')); return $view; }); //Setup a base URI so that all generated URIs include the "tutorial" folder $di->set('url', function () use($config) { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri($config->resource->baseUri); return $url; }); //Start the session the first time when some component request the session service $di->setShared('session', function () { $session = new Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); $di->set('elements', function () {
<?php use Phalcon\Mvc\Url as UrlResolver; use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter; $di = new Phalcon\DI\FactoryDefault(); $di->set('config', $config); $di->set('db', function () use($config) { $connection = new Phalcon\Db\Adapter\Pdo\Mysql($config->database->toArray()); return $connection; }); $di->set('view', function () use($config) { $view = new Phalcon\Mvc\View(); $view->setViewsDir($config->application->viewsDir); $view->registerEngines(array('.volt' => function ($view, $di) use($config) { $volt = new Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_')); return $volt; }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $view; }, true); $di->set('router', function () { return require __DIR__ . '/routes.php'; }); $di->set('session', function () { $session = new Phalcon\Session\Adapter\Files(); $session->start(); return $session; }, true);
public function testTwigMixedEnginePartials() { if ($this->_loadTwig() == false) { return; } $di = new Phalcon\DI(); $view = new Phalcon\Mvc\View(); $view->setDI($di); $view->setViewsDir('unit-tests/views/'); $view->registerEngines(array('.twig' => 'My_Twig_Engine', '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php')); $view->setParamToView('name', 'Sonny'); $view->start(); $view->setRenderLevel(Phalcon\Mvc\View::LEVEL_LAYOUT); $view->render('test12', 'info'); $view->finish(); $this->assertEquals($view->getContent(), 'Well, this is the view content: Hello Sonny.'); }
$di->set('cache', function () { //Cache data for 1 minute $frontCache = new Phalcon\Cache\Frontend\Data(array("lifetime" => 60)); //Memcached connection settings // $cache = new Phalcon\Cache\Backend\File($frontCache, array( // "cacheDir" => "../apps/cache/" //)); // we would use APC cache $cache = new Phalcon\Cache\Backend\Apc($frontCache); return $cache; }); //Registering the view component $di->set('view', function () use($config) { //register event manager for view $eventsManager = new \Phalcon\Events\Manager(); $viewManager = new ViewManager(); $eventsManager->attach('view', $viewManager); $view = new \Phalcon\Mvc\View(); $view->setViewsDir($config->application->viewsDir); $view->registerEngines(array(".phtml" => 'voltService')); $view->setEventsManager($eventsManager); return $view; }); //init applicartion try { $application = new \Phalcon\Mvc\Application(); $application->setDI($di); echo $application->handle()->getContent(); } catch (Phalcon\Exception $e) { echo $e->getMessage(); }
define('APP_ROOT', TESTS_ROOT_DIR . '/fixtures'); $configArray = (require_once TESTS_ROOT_DIR . '/config.php'); $_SERVER['HTTP_HOST'] = 'vegas.dev'; $_SERVER['REQUEST_URI'] = '/'; $config = new \Phalcon\Config($configArray); $di = new Phalcon\DI\FactoryDefault(); $di->set('config', $config); $di->set('collectionManager', function () use($di) { return new \Phalcon\Mvc\Collection\Manager(); }, true); $di->set('collectionManager', function () { return new \Phalcon\Mvc\Collection\Manager(); }); $view = new \Phalcon\Mvc\View(); $view->registerEngines(array('.volt' => function ($this, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($this, $di); $volt->setOptions(array('compiledPath' => TESTS_ROOT_DIR . '/fixtures/cache/', 'compiledSeparator' => '_')); return $volt; }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php')); $di->set('view', $view); $di->set('mongo', function () use($config) { $mongo = new \MongoClient(); return $mongo->selectDb($config->mongo->db); }, true); $di->set('modelManager', function () use($di) { return new \Phalcon\Mvc\Model\Manager(); }, true); $di->set('db', function () use($config) { return new \Phalcon\Db\Adapter\Pdo\Mysql($config->db->toArray()); }, true); Phalcon\DI::setDefault($di);
private function init_view($options = array()) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(APPFULLPATH . $this->_conf->phalcon->viewsDir); $view->registerEngines(array($this->_conf->volt->extn => function ($view, $di) { //$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt = new \Rpp\Extend\Volt($view, $di); $volt->setOptions(array("compiledPath" => "/u/cache/", "compileAlways" => true)); $compiler = $volt->getCompiler(); $compiler->addFunction('getNode', '\\Rpp\\Services\\Get\\Content::node'); $compiler->addFunction('getPart', '\\Rpp\\Services\\Get\\Content::part'); $compiler->addFunction('getGrilla', '\\Rpp\\Services\\Get\\Destacados::portada'); $compiler->addFunction('nUrl', '\\Rpp\\Services\\Get\\Content::nurl'); $compiler->addFunction('gNode', '\\Rpp\\Services\\Get\\Article::node'); $compiler->addFunction('gPart', '\\Rpp\\Services\\Get\\Article::part'); $compiler->addFunction('pUrl', '\\Rpp\\Services\\Get\\Article::nurl'); $compiler->addFunction('TrackUrl', '\\Rpp\\Services\\Get\\UrlTrack::add_params'); $compiler->addFunction('TrackUrlSocial', '\\Rpp\\Services\\Get\\UrlTrack::add_params_social'); $compiler->addFunction('cdn_image', '\\Rpp\\Services\\Get\\UrlMedia::image'); $compiler->addFunction('cdn_replace', '\\Rpp\\Services\\Get\\UrlMedia::replace_cdn'); $compiler->addFunction('df_match', '\\Rpp\\Services\\Get\\Resultados::match'); $compiler->addFunction('df_campeonato', '\\Rpp\\Services\\Get\\Resultados::campeonato'); $compiler->addFunction('df_torneo', '\\Rpp\\Services\\Get\\Dfactory::torneo'); $compiler->addFunction('clima', '\\Rpp\\Services\\Get\\Climas::get'); return $volt; })); $view->setVar("trackurl", true); $view->setVar("host", $this->_conf->server->host); $view->setVar("eversion", $this->_conf->estaticos->version); $view->setVar("ehost", $this->_conf->estaticos->host); $this->_di->set('view', $view); }