19#[\AllowDynamicProperties()]
32 abstract public function index();
39 $this->view =
new View ();
67 public function loadView(
string $viewName, $pData =
null,
bool $asString =
false) {
69 $this->view->setVars($pData);
71 return $this->view->render($viewName, $asString);
118 if (!\headers_sent()) {
119 \header(
'HTTP/1.1 401 Unauthorized',
true, 401);
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;
138 $u[
'params'] = [$params];
152 public function redirectToRoute(
string $routeName, $parameters = [],
bool $initialize =
false,
bool $finalize =
false) {
154 if ($infos !==
false) {
155 if (isset ($infos [
'controller'])) {
156 $this->forward($infos [
'controller'], $infos [
'action'] ??
'index', $parameters, $initialize, $finalize);
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);
163 throw new RouterException (
"Route {$routeName} not found for method {$method}", 404);
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)
static getControllerSimpleName()
Returns the class simple name of the active controller.
static runAction(array &$u, bool $initialize=true, bool $finalize=true)
Runs an action on a controller.
static getViewNameFileExtension()
Returns the extension for view files.
static getAction()
Returns tha active action.
Manager for a Domain Driven Design approach.