added support for named views.

This commit is contained in:
Taylor Otwell 2011-07-21 23:30:52 -05:00
parent 198e95910d
commit c1ca97d9fb
3 changed files with 59 additions and 5 deletions

View File

@ -0,0 +1,30 @@
<?php
return array(
/*
|--------------------------------------------------------------------------
| Named Views
|--------------------------------------------------------------------------
|
| Here you can define all of the named views for your application.
|
| Once you have defined the named view, you can create a View instance for
| that view using the View::of dynamic static method.
|
| For example, if you define a named view named "layout", you could create
| an instance of that View by calling: View::of_layout().
|
| For more info, check out: http://laravel.com/docs/start/views#named
|
| Note: The view path should be relative to the application/view directory.
|
*/
'names' => array(
'home' => 'home/index',
),
);

View File

@ -27,7 +27,7 @@ public function __construct($response)
* @param string $method
* @param int $status
* @param bool $https
* @return Response
* @return Redirect
*/
public static function to($url, $method = 'location', $status = 302, $https = false)
{

View File

@ -16,6 +16,13 @@ class View {
*/
public $data = array();
/**
* The path to the view.
*
* @var string
*/
public $path;
/**
* Create a new view instance.
*
@ -27,6 +34,7 @@ public function __construct($view, $data = array())
{
$this->view = $view;
$this->data = $data;
$this->path = $this->find();
}
/**
@ -38,7 +46,7 @@ public function __construct($view, $data = array())
*/
public static function make($view, $data = array())
{
return new self($view, $data);
return new static($view, $data);
}
/**
@ -61,9 +69,7 @@ public function get()
ob_start();
$path = $this->find();
try { include $path; } catch (\Exception $e) { Error::handle($e); }
try { include $this->path; } catch (\Exception $e) { Error::handle($e); }
return ob_get_clean();
}
@ -105,6 +111,24 @@ public function bind($key, $value)
return $this;
}
/**
* Magic Method for creating named view instances.
*/
public static function __callStatic($method, $parameters)
{
if (strpos($method, 'of_') === 0)
{
$views = Config::get('view.names');
if ( ! array_key_exists($view = substr($method, 3), $views))
{
throw new \Exception("Named view [$view] is not defined.");
}
return static::make($views[$view], (isset($parameters[0]) and is_array($parameters[0])) ? $parameters[0] : array());
}
}
/**
* Magic Method for getting items from the view data.
*/