Example #1
0
 /**
  * Construct JSON string (and also set HTTP header as 'application/json').
  *
  * @param array $data Data in array format
  *
  */
 public function renderJSON($data = array())
 {
     $json = json_encode($data);
     $this->http->header(array('Content-Length' => strlen($json), 'Content-type' => 'application/json;'));
     print $json;
     // Exit application (normally)
     exit(0);
 }
Example #2
0
 /**
  * Request dispatcher.
  *
  * @return mixed Controller/action response
  *
  */
 private function dispatch()
 {
     if ($this->route->isActionClosure()) {
         $controller = 'ClosureContext';
         $action = 'bind';
     } else {
         $controller = $this->route->controller();
         $controller = $controller . Configuration::CONTROLLER_NAME_SUFFIX;
         $action = $this->route->action();
     }
     if (class_exists($controller, true)) {
         // Load reflection of controller/class
         $this->container->reflectionLoad($controller);
         $withConstructor = false;
         $dependencies = array();
         $constructor = $this->container->reflectionGetConstructor();
         // Check if class has a contructor method (either "__construct" or method that is the same name as its class)
         if (isset($constructor->name)) {
             $dependencies = $this->container->reflectionGetMethodDependencies($constructor->name);
             if ($constructor->name == '__construct') {
                 // $skipReflection = true;
                 $withConstructor = true;
             }
         }
         // Create class instance (object)
         $obj = $this->container->reflectionCreateInstance($dependencies, $withConstructor);
         // Prepare/set Closure function
         if ($this->route->isActionClosure()) {
             $obj->set($this->route->getClosure());
         }
         // Check if object method exists
         if (method_exists($obj, $action)) {
             // Check if action method needs dependency
             $dependencies = $this->container->reflectionGetMethodDependencies($action);
             // Call the controller's action method
             call_user_func_array(array($obj, $action), $dependencies);
         } else {
             // No action method found!
             if (!$this->config->application['production']) {
                 $this->http->header($this->http->getValue('SERVER_PROTOCOL') . ' 404 Not Found', true, 404);
                 throw new Exception('Cannot find method "' . $action . '" in controller class "' . $controller . '"');
             } else {
                 $this->view->layout('error')->page('error.404')->render();
             }
         }
     } else {
         // No controller class found!
         if (!$this->config->application['production']) {
             $this->http->header($this->http->getValue('SERVER_PROTOCOL') . ' 404 Not Found', true, 404);
             throw new Exception('Cannot find controller class "' . $controller . '"');
         } else {
             $this->view->layout('error')->page('error.404')->render();
         }
     }
 }