added section class. refactored session for ioc usage. improved controller. added yielding and sections to blade. general blade improvements.

This commit is contained in:
Taylor Otwell
2011-11-08 20:08:20 -06:00
parent d05f4fa0db
commit a1e82d2166
17 changed files with 440 additions and 100 deletions

View File

@@ -1,11 +1,20 @@
<?php namespace Laravel\Routing;
use Laravel\IoC;
use Laravel\View;
use Laravel\Request;
use Laravel\Redirect;
use Laravel\Response;
abstract class Controller {
/**
* The layout being used by the controller.
*
* @var string
*/
public $layout;
/**
* The filters assigned to the controller.
*
@@ -26,7 +35,7 @@ abstract class Controller {
* @param array $parameters
* @return Response
*/
public static function call($destination, $parameters = array())
public static function _call($destination, $parameters = array())
{
if (strpos($destination, '@') === false)
{
@@ -35,14 +44,14 @@ abstract class Controller {
list($controller, $method) = explode('@', $destination);
$controller = static::resolve($controller);
$controller = static::_resolve($controller);
if (is_null($controller))
{
return Response::error('404');
}
return $controller->execute($method, $parameters);
return $controller->_execute($method, $parameters);
}
/**
@@ -52,14 +61,13 @@ abstract class Controller {
* @param string $controller
* @return Controller
*/
public static function resolve($controller)
public static function _resolve($controller)
{
if ( ! static::load($controller)) return;
if ( ! static::_load($controller)) return;
// If the controller is registered in the IoC container, we will
// resolve it out of the container. Using constructor injection
// on controllers via the container allows more flexible and
// testable development of applications.
// If the controller is registered in the IoC container, we will resolve
// it out of the container. Using constructor injection on controllers
// via the container allows more flexible and testable applications.
if (IoC::container()->registered('controllers.'.$controller))
{
return IoC::container()->resolve('controllers.'.$controller);
@@ -67,7 +75,17 @@ abstract class Controller {
$controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
return new $controller;
$controller = new $controller;
// If the controller has specified a layout to be used when rendering
// views, we will instantiate the layout instance and set it to the
// layout property, replacing the string layout name.
if ( ! is_null($controller->layout))
{
$controller->layout = View::make($controller->layout);
}
return $controller;
}
/**
@@ -76,7 +94,7 @@ abstract class Controller {
* @param string $controller
* @return bool
*/
protected static function load($controller)
protected static function _load($controller)
{
$controller = strtolower(str_replace('.', '/', $controller));
@@ -97,9 +115,9 @@ abstract class Controller {
* @param array $parameters
* @return Response
*/
public function execute($method, $parameters = array())
public function _execute($method, $parameters = array())
{
if (static::hidden($method))
if (static::_hidden($method))
{
return Response::error('404');
}
@@ -108,19 +126,30 @@ abstract class Controller {
// "before" filters return a response, it will be considered the
// response to the request and the controller method will not be
// used to handle the request to the application.
$response = Filter::run($this->filters('before', $method), array(), true);
$response = Filter::run($this->gather_filters('before', $method), array(), true);
if (is_null($response))
{
$response = call_user_func_array(array($this, "action_{$method}"), $parameters);
// If the controller has specified a layout view. The response
// returned by the controller method will be bound to that view
// and the layout will be considered the response.
if ( ! is_null($this->layout) and $this->_viewable($response))
{
$response = $this->layout->with('content', $response);
}
}
// The after filter and the framework expects all responses to
// be instances of the Response class. If the method did not
// return an instsance of Response, we will make on now.
if ( ! $response instanceof Response) $response = new Response($response);
if ( ! $response instanceof Response)
{
$response = new Response($response);
}
Filter::run($this->filters('after', $method), array($response));
Filter::run($this->gather_filters('after', $method), array($response));
return $response;
}
@@ -131,29 +160,91 @@ abstract class Controller {
* @param string $method
* @return bool
*/
protected static function hidden($method)
protected static function _hidden($method)
{
return $method == 'before' or $method == 'after' or strncmp($method, '_', 1) == 0;
$hidden = array('before', 'after', 'register_filters', 'gather_filters');
return strncmp($method, '_', 1) == 0 or in_array($method, $hidden);
}
/**
* Set filters on the controller's methods.
* Deteremine if a given response is considered "viewable".
*
* This is primarily used to determine which types of responses should be
* bound to the controller's layout and which should not. We do not want
* to bind redirects and file downloads to the layout, as this obviously
* would not make any sense.
*
* @param mixed $response
* @return bool
*/
protected function _viewable($response)
{
if ($response instanceof Response)
{
if ($response instanceof Redirect)
{
return false;
}
elseif ($response->headers['Content-Description'] == 'File Transfer')
{
return false;
}
}
return true;
}
/**
* Register "before" filters on the controller's methods.
*
* Generally, this method will be used in the controller's constructor.
*
* <code>
* // Set a "foo" before filter on the controller
* $this->filter('before', 'foo');
* $this->before_filter('foo');
*
* // Set several filters on an explicit group of methods
* $this->filter('after', 'foo|bar')->only(array('user', 'profile'));
* $this->before_filter('foo|bar')->only(array('user', 'profile'));
* </code>
*
* @param string|array $filters
* @return Filter_Collection
*/
public function before($filters)
{
return $this->register_filters('before', $filters);
}
/**
* Register "after" filters on the controller's methods.
*
* Generally, this method will be used in the controller's constructor.
*
* <code>
* // Set a "foo" after filter on the controller
* $this->after_filter('foo');
*
* // Set several filters on an explicit group of methods
* $this->after_filter('foo|bar')->only(array('user', 'profile'));
* </code>
*
* @param string|array $filters
* @return Filter_Collection
*/
public function after($filters)
{
return $this->register_filters('after', $filters);
}
/**
* Set filters on the controller's methods.
*
* @param string $name
* @param string|array $filters
* @return Filter_Collection
*/
public function filter($name, $filters)
protected function register_filters($name, $filters)
{
$this->filters[] = new Filter_Collection($name, $filters);
@@ -167,7 +258,7 @@ abstract class Controller {
* @param string $method
* @return array
*/
protected function filters($name, $method)
protected function gather_filters($name, $method)
{
$filters = array();