123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php namespace Laravel\Routing;
- use Laravel\IoC;
- use Laravel\View;
- use Laravel\Request;
- use Laravel\Redirect;
- use Laravel\Response;
- abstract class Controller {
-
- public $layout;
-
- protected $filters = array();
-
- public static function call($destination, $parameters = array())
- {
- if (strpos($destination, '@') === false)
- {
- throw new \Exception("Route delegate [{$destination}] has an invalid format.");
- }
- list($controller, $method) = explode('@', $destination);
- $controller = static::resolve($controller);
- if (is_null($controller))
- {
- return Response::error('404');
- }
- return $controller->execute($method, $parameters);
- }
-
- public static function resolve($controller)
- {
- if ( ! static::load($controller)) return;
-
-
-
- if (IoC::registered('controllers.'.$controller))
- {
- return IoC::resolve('controllers.'.$controller);
- }
- $controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
- $controller = new $controller;
-
-
-
- if ( ! is_null($controller->layout))
- {
- $controller->layout = View::make($controller->layout);
- }
- return $controller;
- }
-
- protected static function load($controller)
- {
- $controller = strtolower(str_replace('.', '/', $controller));
- if (file_exists($path = CONTROLLER_PATH.$controller.EXT))
- {
- require_once $path;
- return true;
- }
- return false;
- }
-
- public function execute($method, $parameters = array())
- {
-
-
-
-
- $response = Filter::run($this->filters('before', $method), array(), true);
- if (is_null($response))
- {
- $response = call_user_func_array(array($this, "action_{$method}"), $parameters);
-
-
-
- if (is_null($response) and ! is_null($this->layout))
- {
- $response = $this->layout;
- }
- }
-
-
-
- if ( ! $response instanceof Response)
- {
- $response = new Response($response);
- }
- Filter::run($this->filters('after', $method), array($response));
- return $response;
- }
-
- protected function filter($name, $filters)
- {
- $this->filters[$name][] = new Filter_Collection($name, $filters);
- return $this->filters[$name][count($this->filters[$name]) - 1];
- }
-
- protected function filters($name, $method)
- {
- if ( ! isset($this->filters[$name])) return array();
- $filters = array();
- foreach ($this->filters[$name] as $filter)
- {
- if ($filter->applies($method))
- {
- $filters = array_merge($filters, $filter->filters);
- }
- }
- return array_unique($filters);
- }
-
- public function __call($method, $parameters)
- {
- return Response::error('404');
- }
-
- public function __get($key)
- {
- if (IoC::registered($key))
- {
- return IoC::resolve($key);
- }
- throw new \Exception("Attempting to access undefined property [$key] on controller.");
- }
- }
|