Skip to content

Instantly share code, notes, and snippets.

@juice49
Created September 16, 2015 10:39
Show Gist options
  • Save juice49/e2f13c52550e5e90c80f to your computer and use it in GitHub Desktop.
Save juice49/e2f13c52550e5e90c80f to your computer and use it in GitHub Desktop.
ZF2 Flash Data Plugin
<?php
namespace App\Controller\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Session\Container;
use Zend\Session\ManagerInterface as Manager;
class Flash extends AbstractPlugin {
protected $container;
protected $session;
const GET_SYMBOL = '__flash-plugin-get-data';
public function __invoke($key = null, $value = self::GET_SYMBOL) {
if(!$key) {
return $this;
}
return $value === self::GET_SYMBOL ?
$this->get($key) :
$this->set($key, $value);
}
public function get($key) {
return $this->getContainer()->$key;
}
public function set($key, $value) {
$container = $this->getContainer();
$container->setExpirationHops(1, null);
$container->$key = $value;
return $this;
}
protected function getSessionManager() {
if(!$this->session instanceof Manager) {
$this->session = Container::getDefaultManager();
}
return $this->session;
}
protected function getContainer() {
if($this->container instanceof Container) {
return $this->container;
}
$manager = $this->getSessionManager();
$this->container = new Container('Flash', $manager);
return $this->container;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment