summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Jasny/Controller.php25
-rw-r--r--src/Jasny/View.php10
-rw-r--r--src/Jasny/View/Twig.php24
3 files changed, 47 insertions, 12 deletions
diff --git a/src/Jasny/Controller.php b/src/Jasny/Controller.php
index f9dd0eb..809b989 100644
--- a/src/Jasny/Controller.php
+++ b/src/Jasny/Controller.php
@@ -22,15 +22,36 @@ abstract class Controller
$this->router = $router;
}
+
+ /**
+ * Returns the HTTP referer if it is on the current host.
+ *
+ * <code>
+ * $this->redirect($this->localReferer() ?: '/'); // Back to previous page on our website
+ * </code>
+ *
+ * @return string
+ */
+ protected function localReferer()
+ {
+ return !empty($_SERVER['HTTP_REFERER']) && parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) == $_SERVER['HTTP_HOST'] ?
+ $_SERVER['HTTP_REFERER'] : null;
+ }
+
+
/**
* Show a view.
*
* @param string $name Filename of Twig template
* @param array $context Data
*/
- protected function view($name, $context = array())
+ protected function view($name=null, $context=[])
{
- header('Content-type: text/html; charset=utf-8');
+ if (isset($this->router)) {
+ if (!isset($name)) $name = $this->router()->get('controller') . '/' . $this->router()->get('action');
+ View::addGlobal('current_route', $this->router->getRoute());
+ }
+
View::load($name)->display($context);
}
diff --git a/src/Jasny/View.php b/src/Jasny/View.php
index e3e204f..5a1967e 100644
--- a/src/Jasny/View.php
+++ b/src/Jasny/View.php
@@ -99,7 +99,7 @@ abstract class View
*
* @return array
*/
- public static function getFlash()
+ public static function useFlash()
{
if (isset($_SESSION['flash'])) {
self::$flash = $_SESSION['flash'];
@@ -108,4 +108,12 @@ abstract class View
return self::$flash;
}
+
+ /**
+ * Add a global variable to the view.
+ *
+ * @param string $name Variable name
+ * @param mixed $value
+ */
+ abstract public static function addGlobal($name, $value);
}
diff --git a/src/Jasny/View/Twig.php b/src/Jasny/View/Twig.php
index faf2458..1b750e0 100644
--- a/src/Jasny/View/Twig.php
+++ b/src/Jasny/View/Twig.php
@@ -2,8 +2,6 @@
namespace Jasny\View;
-use \Jasny\Router;
-
/**
* View using Twig
*/
@@ -13,12 +11,6 @@ class Twig extends \Jasny\View
protected $template;
- /**
- * Cached flash message
- * @var array
- */
- protected static $flash;
-
/** @var \Twig_Environment */
protected static $environment;
@@ -53,6 +45,7 @@ class Twig extends \Jasny\View
*/
public function display($context)
{
+ header('Content-type: text/html; charset=utf-8');
$this->template->display($context);
}
@@ -75,7 +68,7 @@ class Twig extends \Jasny\View
$twig->setCache($cache);
// Add filters and extensions http://twig.sensiolabs.org/doc/api.html#using-extensions
- $twig->addFunction(new \Twig_SimpleFunction('flash', [__CLASS__, 'getFlash']));
+ $twig->addFunction(new \Twig_SimpleFunction('useFlash', [__CLASS__, 'useFlash']));
if (class_exists('Jasny\Twig\DateExtension')) $twig->addExtension(new \Jasny\Twig\DateExtension());
if (class_exists('Jasny\Twig\PcreExtension')) $twig->addExtension(new \Jasny\Twig\PcreExtension());
@@ -91,10 +84,23 @@ class Twig extends \Jasny\View
/**
* Get Twig environment
+ *
+ * @return \Twig_Environment
*/
public static function getEnvironment()
{
if (!isset(static::$environment)) static::init();
return static::$environment;
}
+
+ /**
+ * Add a global variable to the view.
+ *
+ * @param string $name Variable name
+ * @param mixed $value
+ */
+ public static function addGlobal($name, $value)
+ {
+ static::getEnvironment()->addGlobal($name, $value);
+ }
}