We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Volt - flashSession

I have to add flashSession method to volt using $compiler->addFunction ? Using twig i had to add it. It shouldn't be automatically appended to content() ?

edited Mar '14

Hi, If DI is available for Volt you can use flashSession in your templates using registered services

{{ flashSession.output() }}
$di->set('view', function() {
    $view = new \Phalcon\Mvc\View();
    $view->serViewsDir("../app/views");
    $view->registerEngines(array(
        ".volt" => function ($view, $di) {
            $volt = new \Phalcon\Mvc\View\Engine\Volt($view,$di);
            return $volt;
        }
    ));
    return $view;
}

$di->set(
    'flashSession',
    function () {
        return new Phalcon\Flash\Session(array(
            'error' => 'alert alert-dismissable alert-danger',
            'success' => 'alert alert-dismissable alert-success',
            'notice' => 'alert alert-dismissable alert-info',
        ));
    }
);


6.2k

I'm having trouble using flashSession as well

I've added this to the (base) view: {{ flashSession.output() }}

But I can't get the flashSession flashes to show up. Flash direct works as it should.


TestController.php

public function logoutAction() {
    $this->flashSession->success('Yay!');
    $this->response->redirect('index');
  }

services.php

$di = new FactoryDefault();

$di->set('config', function () use ($config) {
    return $config;
});

$di->set('session', function () {
    $session = new SessionAdapter();
    $session->start();

    return $session;
});

$di->set('db', function () use ($config) {
    return new DbAdapter(array(
        'host' => $config->database->host,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname' => $config->database->dbname
    ));
});

$di->set('router', function() {
  require __DIR__ . '/routes.php';
  return $router;
});

$di->set('modelsMetadata', function () {
    return new MetaDataAdapter();
});

$di->set('url', function () use ($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);

    return $url;
}, true);

$di->set('view', function () use ($config) {
    $view = new View();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {
            $volt = new VoltEngine($view, $di);
            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_',
                'prefix' => 'default'
            ));            
            return $volt;
        },
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
    ));

    return $view;
}, true);

$alerts = [
  'success' => 'alert alert-success alert-dismissable fade in',
  'notice' => 'alert alert-info alert-dismissable fade in',
  'warning' => 'alert alert-warning alert-dismissable fade in',
  'error' => 'alert alert-danger alert-dismissable fade in'
];

$di->set('flash', function() use ($alerts) {
    return new \Phalcon\Flash\Direct($alerts);
});

$di->set('flashSession', function() use ($alerts) {
    return new Phalcon\Flash\Session($alerts);
});