本文整理汇总了PHP中Phalcon\Mvc\View::setEventsManager方法的典型用法代码示例。如果您正苦于以下问题:PHP View::setEventsManager方法的具体用法?PHP View::setEventsManager怎么用?PHP View::setEventsManager使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phalcon\Mvc\View
的用法示例。
在下文中一共展示了View::setEventsManager方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: factory
/**
* Create view instance.
* If no events manager provided - events would not be attached.
*
* @param DIBehaviour $di DI.
* @param Config $config Configuration.
* @param string $viewsDirectory Views directory location.
* @param Manager|null $em Events manager.
*
* @return View
*/
public static function factory($di, $config, $viewsDirectory, $em = null)
{
$view = new PhView();
$volt = new PhVolt($view, $di);
$volt->setOptions(["compiledPath" => $config->global->view->compiledPath, "compiledExtension" => $config->global->view->compiledExtension, 'compiledSeparator' => $config->global->view->compiledSeparator, 'compileAlways' => $config->global->view->compileAlways]);
$compiler = $volt->getCompiler();
$compiler->addExtension(new EnViewExtension());
$compiler->addFilter('floor', 'floor');
$compiler->addFunction('range', 'range');
$compiler->addFunction('in_array', 'in_array');
$compiler->addFunction('count', 'count');
$compiler->addFunction('str_repeat', 'str_repeat');
$view->registerEngines(['.volt' => $volt])->setRenderLevel(PhView::LEVEL_ACTION_VIEW)->setViewsDir($viewsDirectory);
// Attach a listener for type "view".
if ($em) {
$em->attach("view", function ($event, $view) use($di, $config) {
if ($config->global->profiler && $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') {
throw new Exception('View not found - "' . $view->getActiveRenderPath() . '"');
}
});
$view->setEventsManager($em);
}
$di->set('view', $view);
return $view;
}
示例2: registerServices
/**
* 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;
});
}
示例3: boot
/**
* @param $container
*/
public function boot(Container $container)
{
$container->setShared('view', function () {
$view = new PhalconView();
$view->setEventsManager($this->getShared('eventsManager'));
$view->setViewsDir($this->path('views'));
$view->registerEngines(['.volt' => 'volt', '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php']);
return $view;
});
$container->setShared('volt', function () {
$volt = new Volt($this->get('view'));
$volt->setOptions(['compiledPath' => $this->tmpPath() . '/cache/volt/', 'compiledSeparator' => '_', 'compileAlways' => true]);
$volt->getCompiler()->addExtension(new \Pails\Extensions\Volt());
return $volt;
});
}
示例4: registerServices
/**
* Register the services here to make them general
* or register in the ModuleDefinition to make them module-specific
*/
public function registerServices(DiInterface $di)
{
$di->set('view', function () {
$view = new View();
$view->setViewsDir(__DIR__ . '/views/');
$view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]);
$view->registerEngines(['.volt' => 'volt']);
// Create an event manager
$eventsManager = new EventsManager();
$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;
});
}
示例5: testGetActiveRenderPath
/**
* Tests the View::getActiveRenderPath
*
* @issue 12139
* @author Serghei Iakovlev <serghei@phalconphp.com>
* @since 2014-08-14
*/
public function testGetActiveRenderPath()
{
$this->specify('The View::getActiveRenderPath returns unexpected result', function () {
$view = new View();
$eventsManager = new Manager();
$eventsManager->attach('view', new ViewAfterRenderListener());
$view->setViewsDir(PATH_DATA . 'views');
$view->setRenderLevel(View::LEVEL_ACTION_VIEW);
$view->setEventsManager($eventsManager);
expect($view->getActiveRenderPath())->equals('');
$view->start();
$view->render('test15', 'index');
$view->finish();
$view->getContent();
expect($view->getActiveRenderPath())->equals(PATH_DATA . 'views' . DIRECTORY_SEPARATOR . 'test15' . DIRECTORY_SEPARATOR . 'index.phtml');
$view->setViewsDir([PATH_DATA . 'views', PATH_DATA . 'views2']);
expect($view->getActiveRenderPath())->equals([PATH_DATA . 'views' . DIRECTORY_SEPARATOR . 'test15' . DIRECTORY_SEPARATOR . 'index.phtml']);
});
}
示例6: registerServices
/**
* Register the services here to make them general or register in the ModuleDefinition to make them module-specific
*/
public function registerServices(\Phalcon\DiInterface $di)
{
/**
* Read configuration
*/
$config = (include __DIR__ . "/config/config.php");
/**
* Setting up the view component
*/
// The URL component is used to generate all kind of urls in the application
$di->set('url', function () {
$url = new Url();
$url->setBaseUri('/');
return $url;
});
/**
* Setting up the view component
*/
$di->set('view', function () use($config) {
$view = new View();
$view->setViewsDir($config->application->view->viewsDir);
$view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]);
$view->registerEngines(['.volt' => function () use($view, $config) {
$volt = new Volt($view);
$volt->setOptions(['compiledPath' => $config->application->view->compiledPath, 'compiledSeparator' => $config->application->view->compiledSeparator, 'compiledExtension' => $config->application->view->compiledExtension, 'compileAlways' => true]);
return $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;
});
}
示例7: initView
/**
* Initialize the View.
*
* Setting up the view component.
*
*
* @param DiInterface $di Dependency Injector
* @param Config $config App config
* @param EventsManager $em Events Manager
*
* @return Loader
*/
protected function initView(DiInterface $di, Config $config, EventsManager $em)
{
$di->set('view', function () use($di, $config, $em) {
$view = new View();
$view->registerEngines(['.volt' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$voltConfig = $config->get('volt')->toArray();
$options = ['compiledPath' => $voltConfig['cacheDir'], 'compiledExtension' => $voltConfig['compiledExt'], 'compiledSeparator' => $voltConfig['separator'], 'compileAlways' => ENV_DEVELOPMENT === APPLICATION_ENV && $voltConfig['forceCompile']];
$volt->setOptions($options);
$compiler = $volt->getCompiler();
$compiler->addFunction('number_format', function ($resolvedArgs) {
return 'number_format(' . $resolvedArgs . ')';
});
$compiler->addFunction('chr', function ($resolvedArgs) {
return 'chr(' . $resolvedArgs . ')';
});
return $volt;
}, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php']);
$view->setViewsDir($config->get('application')->viewsDir);
$em->attach('view', function ($event, $view) use($di, $config) {
/**
* @var LoggerInterface $logger
* @var View $view
* @var Event $event
* @var DiInterface $di
*/
$logger = $di->get('logger');
$logger->debug(sprintf('Event %s. Path: %s', $event->getType(), $view->getActiveRenderPath()));
if ('notFoundView' == $event->getType()) {
$message = sprintf('View not found: %s', $view->getActiveRenderPath());
$logger->error($message);
throw new ViewException($message);
}
});
$view->setEventsManager($em);
return $view;
});
}
示例8: testIssue907
public function testIssue907()
{
$view = new View();
$view->setBasePath(__DIR__ . '/../');
$view->setViewsDir('unit-tests/views/');
$listener = new \ViewAfterRenderListener();
$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach('view', $listener);
$view->setEventsManager($eventsManager);
$view->start();
$view->render('test3', 'other');
$view->finish();
$this->assertEquals($view->getContent(), '<html>lolhere</html>' . PHP_EOL);
$this->assertEquals('1,3,6', $listener->getLevels());
$listener->reset();
//Templates
$view->setTemplateAfter('test');
$view->start();
$view->render('test3', 'other');
$view->finish();
$this->assertEquals($view->getContent(), '<html>zuplolhere</html>' . PHP_EOL);
$this->assertEquals('1,3,5,6', $listener->getLevels());
$listener->reset();
$view->cleanTemplateAfter();
//Render Levels
$view->setRenderLevel(View::LEVEL_MAIN_LAYOUT);
$view->start();
$view->render('test3', 'other');
$view->finish();
$this->assertEquals($view->getContent(), '<html>lolhere</html>' . PHP_EOL);
$this->assertEquals('1,3,6', $listener->getLevels());
$listener->reset();
$view->setRenderLevel(View::LEVEL_LAYOUT);
$view->start();
$view->render('test3', 'other');
$view->finish();
$this->assertEquals($view->getContent(), 'lolhere');
$this->assertEquals('1,3', $listener->getLevels());
$listener->reset();
$view->setRenderLevel(View::LEVEL_ACTION_VIEW);
$view->start();
$view->render('test3', 'other');
$view->finish();
$this->assertEquals($view->getContent(), 'here');
$this->assertEquals('1', $listener->getLevels());
$listener->reset();
}
示例9: function
});
// Setting up the view component
$di->set('view', function () use($di, $eventsManager) {
$config = $di->get('config');
$view = new View($config->toArray());
$view->setViewsDir($config->application->view->viewsDir);
$view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]);
$view->registerEngines(['.volt' => 'volt']);
// 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;
});
// Register the flash service with custom CSS classes
$di->set('flashSession', function () {
$flash = new Session(['error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning']);
return $flash;
});
// Database connection is created based in the parameters defined in the configuration file
$di->set('db', function () use($di) {
$config = $di->get('config');
$connection = new Mysql(['host' => $config->database->mysql->host, 'username' => $config->database->mysql->username, 'password' => $config->database->mysql->password, 'dbname' => $config->database->mysql->dbname, 'options' => [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $config->database->mysql->charset]]);
if ($config->application->debug) {
$eventsManager = new EventsManager();
$logger = new FileLogger(ROOT_DIR . 'content/logs/db.log');
//Listen all the database events
示例10: testGetCurrentRenderLevel
/**
* Tests the View::getCurrentRenderLevel
*
* @issue 907
* @author Volodymyr Kolesnykov <volodymyr@wildwolf.name>
* @since 2013-10-03
*/
public function testGetCurrentRenderLevel()
{
$this->specify('The View listener does not fetch current render level correctly', function () {
$view = new View();
$listener = new AfterRenderListener();
$eventsManager = new Manager();
$eventsManager->attach('view', $listener);
$view->setViewsDir(PATH_DATA . 'views' . DIRECTORY_SEPARATOR);
$view->setEventsManager($eventsManager);
$view->start();
$view->render('test3', 'other');
$view->finish();
expect($view->getContent())->equals("<html>lolhere</html>\n");
expect($listener->getLevels())->equals('1,3,5');
$listener->reset();
$view->setTemplateAfter('test');
$view->start();
$view->render('test3', 'other');
$view->finish();
expect($view->getContent())->equals("<html>zuplolhere</html>\n");
expect($listener->getLevels())->equals('1,3,4,5');
$listener->reset();
$view->cleanTemplateAfter();
$view->setRenderLevel(View::LEVEL_MAIN_LAYOUT);
$view->start();
$view->render('test3', 'other');
$view->finish();
expect($view->getContent())->equals("<html>lolhere</html>\n");
expect($listener->getLevels())->equals('1,3,5');
$listener->reset();
$view->setRenderLevel(View::LEVEL_LAYOUT);
$view->start();
$view->render('test3', 'other');
$view->finish();
expect($view->getContent())->equals('lolhere');
expect($listener->getLevels())->equals('1,3');
$listener->reset();
$view->setRenderLevel(View::LEVEL_ACTION_VIEW);
$view->start();
$view->render('test3', 'other');
$view->finish();
expect($view->getContent())->equals('here');
expect($listener->getLevels())->equals('1');
$listener->reset();
});
}
示例11: registerServices
/**
* Register services
*
* @param DiInterface $di
*/
public final function registerServices(DiInterface $di)
{
$module = $this->module;
$baseControllers = $this->baseControllers;
//Registering a dispatcher
$di->set('dispatcher', function () use($di, &$module, &$baseControllers) {
//Create new Dispatcher
$dispatcher = new PDispatcher();
//Set default namespace to this module
$dispatcher->setModuleName($this->module);
//Set default namespace
$dispatcher->setDefaultNamespace($baseControllers);
//Set default controller
$dispatcher->setDefaultController($this->defaultController);
/**
* Get Event Manager
*
* @var \Phalcon\Events\Manager $eventsManager
*/
$eventsManager = $di->getShared('eventsManager');
//Attach acl in dispatcher
$eventsManager->attach('dispatch', $di->get('acl'));
//Set event manager
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
//Register the view component
$di->set('view', function () use($di, &$module) {
//Create Phalcon\Mvc\View
$view = new ZView();
$template = new ZTemplate($this->module);
//Attach event
$eventsManager = $di->getShared('eventsManager');
if (method_exists($eventsManager, 'attach')) {
$eventsManager->attach('view:beforeRender', $template);
$eventsManager->attach('view:afterRender', $template);
$eventsManager->attach('view:beforeRenderView', $template);
} else {
die(__FILE__ . ' Error: ZCMS cannot render template');
}
//Set view Event
$view->setEventsManager($eventsManager);
//Set view dir
$view->setViewsDir(APP_DIR . '/backend/' . $module . '/views/');
//Register engines
$view->registerEngines(['.volt' => function ($view, $di) {
$volt = new Volt($view, $di);
$volt->setOptions(['compiledPath' => function ($templatePath) {
$templatePath = strstr($templatePath, '/app');
$dirName = dirname($templatePath);
if (!is_dir(ROOT_PATH . '/cache/volt' . $dirName)) {
mkdir(ROOT_PATH . '/cache/volt' . $dirName, 0755, TRUE);
}
return ROOT_PATH . '/cache/volt' . $dirName . '/' . basename($templatePath, '.volt') . '.php';
}, 'compileAlways' => method_exists($di, 'get') ? (bool) $di->get('config')->backendTemplate->compileTemplate : false]);
$compiler = $volt->getCompiler();
$compiler->addFunction('get_sidebar', 'get_sidebar');
$compiler->addFunction('__', '__');
$compiler->addFunction('strtotime', 'strtotime');
$compiler->addFunction('human_timing', 'human_timing');
$compiler->addFunction('moneyFormat', 'moneyFormat');
$compiler->addFunction('number_format', 'number_format');
$compiler->addFunction('change_date_format', 'change_date_format');
$compiler->addFunction('in_array', 'in_array');
return $volt;
}]);
return $view;
}, true);
}
示例12: registerServices
/**
* Register services
*
* @param DiInterface $di
*/
public function registerServices(DiInterface $di)
{
$module = $this->module;
$baseControllers = $this->baseControllers;
//Registering a dispatcher
$di->set('dispatcher', function () use($di, &$module, &$baseControllers) {
/**
* @var \Phalcon\Events\ManagerInterface $eventsManager
*/
$eventsManager = $di->getShared('eventsManager');
$eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
/**
* @var Dispatcher $dispatcher
* @var \Phalcon\Mvc\Dispatcher\Exception $exception
*/
switch ($exception->getCode()) {
case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(['module' => 'index', 'controller' => 'error', 'action' => 'notFound404']);
return false;
}
return true;
});
$dispatcher = new Dispatcher();
//Set default namespace to this module
$dispatcher->setDefaultNamespace($baseControllers);
$dispatcher->setEventsManager($eventsManager);
$dispatcher->setModuleName($module);
return $dispatcher;
});
//Registering the view component
$di->set('view', function () use($di, &$module) {
$view = new View();
$view->setViewsDir(APP_DIR . '/frontend/' . $module . '/views/');
/**
* @var \Phalcon\Events\Manager $eventsManager
*/
$eventsManager = $di->getShared('eventsManager');
$eventsManager->attach('view:beforeRender', new ZFrontTemplate($this->module));
$eventsManager->attach('view:afterRender', new ZFrontTemplate($this->module));
//Set view Event
$view->setEventsManager($eventsManager);
$view->registerEngines(['.volt' => function ($view, $di) {
$volt = new Volt($view, $di);
$volt->setOptions(['compiledPath' => function ($templatePath) {
$templatePath = strstr($templatePath, '/app');
$dirName = dirname($templatePath);
if (!is_dir(ROOT_PATH . '/cache/volt' . $dirName)) {
mkdir(ROOT_PATH . '/cache/volt' . $dirName, 0755, true);
}
return ROOT_PATH . '/cache/volt' . $dirName . '/' . basename($templatePath, '.volt') . '.php';
}, 'compileAlways' => method_exists($di, 'get') ? (bool) $di->get('config')->frontendTemplate->compileTemplate : false, 'stat' => false]);
$compiler = $volt->getCompiler();
$compiler->addFunction('get_sidebar', 'get_sidebar');
$compiler->addFunction('__', '__');
$compiler->addFilter('t', function ($resolvedArgs) {
return '__(' . $resolvedArgs . ')';
});
$compiler->addFunction('strtotime', 'strtotime');
$compiler->addFunction('human_timing', 'human_timing');
$compiler->addFunction('moneyFormat', 'moneyFormat');
$compiler->addFunction('number_format', 'number_format');
$compiler->addFunction('zcms_header', 'zcms_header');
$compiler->addFunction('zcms_header_prefix', 'zcms_header_prefix');
$compiler->addFunction('change_date_format', 'change_date_format');
$compiler->addFunction('in_array', 'in_array');
return $volt;
}]);
return $view;
});
}
示例13: initView
/**
* Initialize the View.
*
* Setting up the view component.
*/
protected function initView()
{
$this->di->set('view', function () {
/** @var DiInterface $this */
$config = $this->getShared('config');
$em = $this->getShared('eventsManager');
$view = new View();
$view->registerEngines(['.volt' => function ($view, $di) {
/** @var DiInterface $this */
$config = $this->getShared('config');
$volt = new VoltEngine($view, $di);
$options = ['compiledPath' => function ($templatePath) {
/** @var DiInterface $this */
$config = $this->getShared('config')->get('volt')->toArray();
$filename = str_replace(['\\', '/'], $config['separator'], trim(substr($templatePath, strlen(BASE_DIR)), '\\/'));
$filename = basename($filename, '.volt') . $config['compiledExt'];
$cacheDir = rtrim($config['cacheDir'], '\\/') . DIRECTORY_SEPARATOR;
if (!is_dir($cacheDir)) {
@mkdir($cacheDir, 0755, true);
}
return rtrim($config['cacheDir'], '\\/') . DIRECTORY_SEPARATOR . $filename;
}, 'compileAlways' => boolval($config->get('volt')->forceCompile)];
$volt->setOptions($options);
$volt->getCompiler()->addFunction('number_format', function ($resolvedArgs) {
return 'number_format(' . $resolvedArgs . ')';
})->addFunction('chr', function ($resolvedArgs) {
return 'chr(' . $resolvedArgs . ')';
});
return $volt;
}, '.phtml' => Php::class]);
$view->setViewsDir($config->get('application')->viewsDir);
$that = $this;
$em->attach('view', function ($event, $view) use($that, $config) {
/**
* @var LoggerInterface $logger
* @var View $view
* @var Event $event
* @var DiInterface $that
*/
$logger = $that->get('logger');
$logger->debug(sprintf('Event %s. Path: %s', $event->getType(), $view->getActiveRenderPath()));
if ('notFoundView' == $event->getType()) {
$message = sprintf('View not found: %s', $view->getActiveRenderPath());
$logger->error($message);
throw new ViewException($message);
}
});
$view->setEventsManager($em);
return $view;
});
}