summaryrefslogtreecommitdiffstats
path: root/src/Controller/Session.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Controller/Session.php')
-rw-r--r--src/Controller/Session.php65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/Controller/Session.php b/src/Controller/Session.php
new file mode 100644
index 0000000..8dc292f
--- /dev/null
+++ b/src/Controller/Session.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace Jasny\Controller;
+
+use Jasny\Controller\Session\Flash;
+
+/**
+ * Use session in the controller
+ */
+trait Session
+{
+ /**
+ * Session
+ * @var array|\ArrayObject
+ */
+ protected $session;
+
+ /**
+ * Flash message
+ * @var Flash
+ */
+ protected $flash;
+
+
+ /**
+ * Get request, set for controller
+ *
+ * @return ServerRequestInterface
+ */
+ abstract protected function getRequest();
+
+
+ /**
+ * Link the session to the session property in the controller
+ */
+ protected function useSession()
+ {
+ $this->session = $this->getRequest()->getAttribute('session');
+
+ if (!isset($this->session)) {
+ $this->session =& $_SESSION;
+ }
+ }
+
+
+ /**
+ * Get an/or set the flash message.
+ *
+ * @param mixed $type flash type, eg. 'error', 'notice' or 'success'
+ * @param mixed $message flash message
+ * @return Flash
+ */
+ public function flash($type = null, $message = null)
+ {
+ if (!isset($this->flash)) {
+ $this->flash = new Flash($this->session);
+ }
+
+ if ($type) {
+ $this->flash->set($type, $message);
+ }
+
+ return $this->flash;
+ }
+}