public function testApplicationModulesDefinitionClosure() { // Creates the autoloader $loader = new \Phalcon\Loader(); $loader->registerNamespaces(array('Frontend\\Controllers' => __DIR__ . '/modules/frontend/controllers/', 'Backend\\Controllers' => __DIR__ . '/modules/backend/controllers/')); $loader->register(); $_GET['_url'] = '/login'; Phalcon\DI::reset(); $di = new Phalcon\DI\FactoryDefault(); $di->set('router', function () { $router = new Phalcon\Mvc\Router(false); $router->add('/index', array('controller' => 'index', 'module' => 'frontend', 'namespace' => 'Frontend\\Controllers\\')); $router->add('/login', array('controller' => 'login', 'module' => 'backend', 'namespace' => 'Backend\\Controllers\\')); return $router; }); $application = new Phalcon\Mvc\Application(); $view = new \Phalcon\Mvc\View(); $application->registerModules(array('frontend' => function ($di) use($view) { $di->set('view', function () use($view) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/modules/frontend/views/'); return $view; }); }, 'backend' => function ($di) use($view) { $di->set('view', function () use($view) { $view->setViewsDir(__DIR__ . '/modules/backend/views/'); return $view; }); })); $application->setDi($di); $this->assertEquals($application->handle()->getContent(), '<html>here</html>' . PHP_EOL); $loader->unregister(); }
/** * 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; }); } }
public function registerServices($di) { $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/'); return $view; }; /** * Register a component */ $di->set('menu', function () { return new \Modules\Frontend\Elements\Menu(); }); $di->set('element', function () { return new \Modules\Frontend\Elements\Element(); }); }
public function testControllers() { $di = new Phalcon\DI(); $di->set('view', function () { $view = new Phalcon\Mvc\View(); $view->setViewsDir('unit-tests/views/'); return $view; }); $di->set('request', function () { return new Phalcon\Http\Request(); }); $di->set('filter', function () { return new Phalcon\Filter(); }); if (!class_exists('Test4Controller', false)) { require __DIR__ . '/controllers/Test4Controller.php'; } $controller = new Test4Controller(); $controller->setDI($di); $_POST['email'] = ';ans@ecom.com'; $this->assertEquals($controller->requestAction(), '*****@*****.**'); $view = $di->getShared('view'); $controller->viewAction(); $this->assertEquals(count($view->getParamsToView()), 1); }
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 function registerServices($di) { /** Read configuration */ $config = (include __DIR__ . "/../configs/config.php"); $di['dispatcher'] = function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultNamespace("Modules\\Admin\\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->setPartialsDir('../../common/partials/'); return $view; }; /** set config */ $di->set('config', function () use($config) { return $config; }, true); /** 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) { $di['view'] = function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/views/'); return $view; }; }
public function registerServices(\Phalcon\DiInterface $di) { $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('unit-tests/modules/frontend/views/'); 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 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; }
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; }); }
public function onWorkerStart() { $loader = new \Phalcon\Loader(); $loader->registerDirs(array(ROOT_PATH . '/application/controller/', ROOT_PATH . '/application/model/')); $loader->register(); $this->di = new \Phalcon\Di\FactoryDefault(); //Registering the view component $this->di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(ROOT_PATH . '/application/views/'); 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(\Phalcon\DiInterface $di) { // Registering a dispatcher $di->set('dispatcher', function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultNamespace('Bishop\\Shop\\Controllers'); return $dispatcher; }); // Registering the view component $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(ROOT_DIR . '/app/shop/views/'); 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; }; }
public function registerServices($di) { $di['dispatcher'] = function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultNamespace("Modules\\Backend\\Controllers"); return $dispatcher; }; /** * Setting up the view component */ $di['view'] = function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/views/'); 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 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(__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(); }); }
/** * 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; }); }
/** * This methods registers the services to be used by the application */ protected function _registerServices() { $di = new \Phalcon\DI(); //Registering a router $di->set('router', function () { return new \Phalcon\Mvc\Router(); }); //Registering a dispatcher $di->set('dispatcher', function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultNamespace('Single\\Controllers\\'); return $dispatcher; }); //Registering a Http\Response $di->set('response', function () { return new \Phalcon\Http\Response(); }); //Registering a Http\Request $di->set('request', function () { return new \Phalcon\Http\Request(); }); //Registering the view component $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../apps/views/'); return $view; }); $di->set('db', function () { return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => "localhost", "username" => "root", "password" => "", "dbname" => "invo")); }); //Registering the Models-Metadata $di->set('modelsMetadata', function () { return new \Phalcon\Mvc\Model\Metadata\Memory(); }); //Registering the Models Manager $di->set('modelsManager', function () { return new \Phalcon\Mvc\Model\Manager(); }); $this->setDI($di); }
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 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 Database(array("host" => "localhost", "username" => "root", "password" => "secret", "dbname" => "invo")); }); }
/** * 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; }
/** * 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' => 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; }
try { $config = new \Phalcon\Config\Adapter\Ini(APP_DIR . '/config/config.ini'); //Register an autoloader $loader = new \Phalcon\Loader(); $loader->registerNamespaces(["Watchlist\\Controllers" => BASE_DIR . '/' . $config->application->controllersDir, "Watchlist\\Views" => BASE_DIR . '/' . $config->application->viewsDir, "Watchlist\\Models" => BASE_DIR . '/' . $config->application->modelsDir, "Watchlist" => BASE_DIR . '/' . $config->application->appLibDir, "Phalcore" => BASE_DIR . '/' . $config->application->librariesDir . "phalcore/"])->register(); //Create a DI $config["di"] = new \Phalcon\DI\FactoryDefault(); $config["di"]->set('session', function () { $session = new \Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); //Setup the view component $config["di"]->set('view', function () use($config) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(BASE_DIR . '/' . $config->application->viewsDir); return $view; }); $config["di"]->set('dispatcher', function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultNamespace('Watchlist\\Controllers'); return $dispatcher; }); //Setup the flash service $config["di"]->set('flash', function () { return new \Phalcon\Flash\Direct(); }); //Route Settings include "../app/config/routes.php"; //DB Settings $config["di"]->set('mongo', function () use($config) {
public function testViewOptions() { $config = array('cache' => array('service' => 'otherCache')); $di = new Phalcon\DI(); $di->set('otherCache', function () { $frontend = new Phalcon\Cache\Frontend\Output(array('lifetime' => 60)); return new Phalcon\Cache\Backend\File($frontend, array('cacheDir' => 'unit-tests/cache/')); }); $view = new Phalcon\Mvc\View($config); $view->setDI($di); $view->setViewsDir('unit-tests/views/'); $date = date("r"); $content = '<html>' . $date . '</html>' . PHP_EOL; $view->setVar("date", $date); $view->start(); $view->cache(true); $view->render('test8', 'other'); $view->finish(); $this->assertEquals($view->getContent(), $content); $view->reset(); sleep(1); $view->setVar("date", date("r")); $view->start(); $view->cache(true); $view->render('test8', 'other'); $view->finish(); $this->assertEquals($view->getContent(), $content); }