Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
Controller.php
Go to the documentation of this file.
1<?php
2
4
9
19#[\AllowDynamicProperties()]
20abstract class Controller {
21
27 protected View $view;
28
32 abstract public function index();
33
38 public function __construct() {
39 $this->view = new View ();
40 }
41
46 public function initialize() {
47 }
48
53 public function finalize() {
54 }
55
67 public function loadView(string $viewName, $pData = null, bool $asString = false) {
68 if (isset ($pData)) {
69 $this->view->setVars($pData);
70 }
71 return $this->view->render($viewName, $asString);
72 }
73
85 public function loadDefaultView($pData = null, bool $asString = false) {
86 return $this->loadView($this->getDefaultViewName(), $pData, $asString);
87 }
88
95 public function getDefaultViewName(): string {
96 if (ThemesManager::getActiveTheme() !== '') {
98 }
99 return DDDManager::getViewNamespace() . Startup::getControllerSimpleName() . "/" . Startup::getAction() . "." . Startup::getViewNameFileExtension();
100 }
101
109 public function isValid($action) {
110 return true;
111 }
112
117 public function onInvalidControl() {
118 if (!\headers_sent()) {
119 \header('HTTP/1.1 401 Unauthorized', true, 401);
120 }
121 }
122
133 public function forward(string $controller, string $action = 'index', $params = [], bool $initialize = false, bool $finalize = false) {
134 $u = ['controller' => $controller, 'action' => $action];
135 if (\is_array($params)) {
136 $u['params'] = $params;
137 } else {
138 $u['params'] = [$params];
139 }
140 Startup::runAction($u, $initialize, $finalize);
141 }
142
152 public function redirectToRoute(string $routeName, $parameters = [], bool $initialize = false, bool $finalize = false) {
153 $infos = Router::getRouteInfoByName($routeName);
154 if ($infos !== false) {
155 if (isset ($infos ['controller'])) {
156 $this->forward($infos ['controller'], $infos ['action'] ?? 'index', $parameters, $initialize, $finalize);
157 } else {
158 $method = \strtolower($_SERVER ['REQUEST_METHOD']);
159 if (isset ($infos [$method])) {
160 $infos = $infos [$method];
161 $this->forward($infos ['controller'], $infos ['action'] ?? 'index', $parameters, $initialize, $finalize);
162 } else {
163 throw new RouterException ("Route {$routeName} not found for method {$method}", 404);
164 }
165 }
166 } else {
167 throw new RouterException ("Route {$routeName} not found", 404);
168 }
169 }
170
176 public function getView(): View {
177 return $this->view;
178 }
179}
Base class for controllers.
__construct()
Constructor initialize $view variable.
loadDefaultView($pData=null, bool $asString=false)
Loads the default view (controllerName/actionName) possibly passing the variables $pdata.
getView()
Returns the associated view instance.
initialize()
Method called before each action Can be override in derived class.
finalize()
Method called after each action Can be override in derived class.
forward(string $controller, string $action='index', $params=[], bool $initialize=false, bool $finalize=false)
Loads the controller $controller and calls its $action method by passing the parameters $params.
onInvalidControl()
Called if isValid () returns false To be override in sub classes.
redirectToRoute(string $routeName, $parameters=[], bool $initialize=false, bool $finalize=false)
Redirect to a route by its name.
isValid($action)
Returns True if access to the controller is allowed To be override in sub classes.
getDefaultViewName()
Returns the default view name for this controller/action i.e ControllerName/actionName....
loadView(string $viewName, $pData=null, bool $asString=false)
Loads the view $viewName possibly passing the variables $pdata.
static getRouteInfoByName($name)
Definition Router.php:163
static getControllerSimpleName()
Returns the class simple name of the active controller.
Definition Startup.php:300
static runAction(array &$u, bool $initialize=true, bool $finalize=true)
Runs an action on a controller.
Definition Startup.php:155
static getViewNameFileExtension()
Returns the extension for view files.
Definition Startup.php:309
static getAction()
Returns tha active action.
Definition Startup.php:318
Manager for a Domain Driven Design approach.
Represents a view.
Definition View.php:17