Ubiquity  2.0.3
php rapid development framework
Controller.php
Go to the documentation of this file.
1 <?php
2 
7 namespace Ubiquity\controllers;
8 
11 
18 abstract class Controller {
24  protected $view;
25 
29  abstract public function index();
30 
35  public function __construct() {
36  $this->view = new View ();
37  }
38 
43  public function initialize() {
44  }
45 
50  public function finalize() {
51  }
52 
66  public function loadView($viewName, $pData = NULL, $asString = false) {
67  if (isset ( $pData ))
68  $this->view->setVars ( $pData );
69  return $this->view->render ( $viewName, $asString );
70  }
71 
82  public function loadDefaultView($pData=NULL,$asString=false){
83  return $this->loadView($this->getDefaultViewName(),$pData,$asString);
84  }
85 
90  public function getDefaultViewName(){
92  }
93 
101  public function isValid($action) {
102  return true;
103  }
104 
109  public function onInvalidControl() {
110  \header ( 'HTTP/1.1 401 Unauthorized', true, 401 );
111  }
112 
128  public function forward($controller, $action = "index", $params = array(), $initialize = false, $finalize = false) {
129  $u = array ($controller,$action );
130  if (\is_array ( $params )) {
131  $u = \array_merge ( $u, $params );
132  } else {
133  $u = \array_merge ( $u, [ $params ] );
134  }
135  return Startup::runAction ( $u, $initialize, $finalize );
136  }
137 
147  public function redirectToRoute($routeName, $parameters = [], $initialize = false, $finalize = false) {
148  $path = Router::getRouteByName ( $routeName, $parameters );
149  if ($path !== false) {
150  $route = Router::getRoute ( $path, false );
151  if ($route !== false) {
152  return $this->forward ( $route [0], $route [1], \array_slice ( $route, 2 ), $initialize, $finalize );
153  } else {
154  throw new RouterException ( "Route {$routeName} not found", 404 );
155  }
156  } else {
157  throw new RouterException ( "Route {$routeName} not found", 404 );
158  }
159  }
160 
165  public function getView() {
166  return $this->view;
167  }
168 }
isValid($action)
Returns True if access to the controller is allowed To be override in sub classes.
Definition: Controller.php:101
loadDefaultView($pData=NULL, $asString=false)
Loads the default view (controllerName/actionName) possibly passing the variables $pdata...
Definition: Controller.php:82
finalize()
Method called after each action Can be override in derived class.
Definition: Controller.php:50
getDefaultViewName()
Returns the default view name for this controller/action i.e ControllerName/actionName.html for the action actionName in ControllerName.
Definition: Controller.php:90
loadView($viewName, $pData=NULL, $asString=false)
Loads the view $viewName possibly passing the variables $pdata.
Definition: Controller.php:66
Base class for controllers.
Definition: Controller.php:18
static getRoute($path, $cachedResponse=true)
Definition: Router.php:35
forward($controller, $action="index", $params=array(), $initialize=false, $finalize=false)
Loads the controller $controller and calls its $action method by passing the parameters $params...
Definition: Controller.php:128
static runAction($u, $initialize=true, $finalize=true)
Definition: Startup.php:87
initialize()
Method called before each action Can be override in derived class.
Definition: Controller.php:43
Represents a view.
Definition: View.php:14
static getRouteByName($name, $parameters=[], $absolute=true)
Returns the generated path from a route.
Definition: Router.php:108
This file is part of Ubiquity framework.
__construct()
Constructor initialize $view variable.
Definition: Controller.php:35
onInvalidControl()
Called if isValid () returns false To be override in sub classes.
Definition: Controller.php:109
redirectToRoute($routeName, $parameters=[], $initialize=false, $finalize=false)
Redirect to a route by its name.
Definition: Controller.php:147