AloFramework documentation
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo

Namespaces

  • Alo
    • Cache
    • CLI
    • Controller
    • Db
    • Exception
    • FileSystem
    • Session
    • Traversables
    • Validators
    • Windows
  • Controller
  • None
  • PHP

Classes

  • AbstractController
  • AbstractErrorController
  • Router
  1 <?php
  2 
  3     namespace Alo\Controller;
  4 
  5     if (!defined('GEN_START')) {
  6         http_response_code(404);
  7     } else {
  8 
  9         /**
 10          * The controller superclass
 11          *
 12          * @author Art <a.molcanovas@gmail.com>
 13          */
 14         abstract class AbstractController {
 15 
 16             /**
 17              * Static reference to the last instance of the class
 18              *
 19              * @var AbstractController
 20              */
 21             static $this;
 22             /**
 23              * Whether to echo contents on object destruct
 24              *
 25              * @var boolean
 26              */
 27             private $echoOnDestruct;
 28 
 29             /**
 30              * Instantiates the class
 31              *
 32              * @param boolean $echoOnDestruct Whether to echo contents on object destruct
 33              *
 34              * @author Art <a.molcanovas@gmail.com>
 35              */
 36             function __construct($echoOnDestruct = true) {
 37                 ob_start();
 38                 $this->echoOnDestruct = (bool)$echoOnDestruct;
 39 
 40                 self::$this = &$this;
 41             }
 42 
 43             /**
 44              * Method to avoid errors. Should always be overridden.
 45              *
 46              * @author Art <a.molcanovas@gmail.com>
 47              */
 48             function index() {
 49                 $this->httpError(404);
 50             }
 51 
 52             /**
 53              * Forces a HTTP error page to be displayed. This does not stop script execution, but prevents further output.
 54              *
 55              * @author Art <a.molcanovas@gmail.com>
 56              *
 57              * @param int $code The HTTP response code
 58              */
 59             protected function httpError($code = 404) {
 60                 $this->echoOnDestruct = true;
 61                 ob_clean();
 62 
 63                 $controller           = \Alo::$router->getErrController();
 64                 $controllerNamespaced = '\Controller\\' . $controller;
 65 
 66                 includeonceifexists(DIR_APP . 'controllers' . DIRECTORY_SEPARATOR . strtolower($controller) . '.php');
 67 
 68                 if (!class_exists($controllerNamespaced, true)) {
 69                     http_response_code((int)$code);
 70                     echo 'HTTP ' . escapeHTML($code) . '.';
 71                 } else {
 72                     \Alo::$controller = new $controllerNamespaced;
 73                     /** @noinspection PhpUndefinedMethodInspection */
 74                     \Alo::$controller->error($code);
 75                     ob_flush();
 76                     $this->echoOnDestruct = false;
 77                 }
 78             }
 79 
 80             /**
 81              * Closure operations
 82              *
 83              * @author Art <a.molcanovas@gmail.com>
 84              */
 85             function __destruct() {
 86                 if ($this->echoOnDestruct) {
 87                     $ob = ob_get_clean();
 88 
 89                     if (\Alo::$router->isCliRequest()) {
 90                         $ob = strip_tags($ob);
 91                     }
 92 
 93                     echo $ob;
 94                 } else {
 95                     ob_end_clean();
 96                 }
 97             }
 98 
 99             /**
100              * Returns if echoOnDestruct is true or false if called without a parameter
101              * or sets it to true/false if the parameter is set
102              *
103              * @param boolean|null $switch The parameter
104              *
105              * @return boolean|AbstractController
106              */
107             protected function echoOnDestruct($switch = null) {
108                 if ($switch === null) {
109                     return $this->echoOnDestruct;
110                 } else {
111                     $this->echoOnDestruct = (bool)$switch;
112 
113                     return $this;
114                 }
115             }
116 
117             /**
118              * Loads a view
119              *
120              * @author Art <a.molcanovas@gmail.com>
121              *
122              * @param string  $name   The name of the view without ".php".
123              * @param array   $params Associative array of parameters to pass on to the view
124              * @param boolean $return If set to TRUE, will return the view, if FALSE,
125              *                        will echo it
126              *
127              * @return null|string
128              */
129             protected function loadView($name, $params = null, $return = false) {
130                 $name = strtolower($name);
131 
132                 if (substr($name, -4) == '.php') {
133                     $name = substr($name, 0, -4);
134                 }
135 
136                 $path = DIR_APP . 'view' . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $name) . '.php';
137 
138                 if (!file_exists($path)) {
139                     phpError('View file for ' . $name . ' could not be found');
140                 } else {
141                     if (is_array($params) && !empty($params)) {
142                         extract($params);
143                     }
144 
145                     if ($return) {
146                         ob_start();
147                     }
148 
149                     //not include_once so the view can be reused
150                     include $path;
151 
152                     if ($return) {
153                         return ob_get_clean();
154                     }
155                 }
156 
157                 return null;
158             }
159 
160         }
161     }
162 
AloFramework documentation API documentation generated byApiGen 2.8.0