Skip to content

Commit 9af8011

Browse files
author
alddesign
committed
v1.0.0-beta.3
Added Views shared data feature Added Controller onRequest feature Added Timezone settings
1 parent 75ba7ce commit 9af8011

File tree

6 files changed

+47
-7
lines changed

6 files changed

+47
-7
lines changed

index.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
die('Damn, run <b>composer update</b> before using EZ-MVC.<br/>Please, <a href="https://getcomposer.org">get composer.</a> and thank me later.');
66
}
77
require ($autoloader); //using composers autoloader file
8+
89
session_start();
910

11+
//Set default timezone as soon as possible
12+
date_default_timezone_set(Alddesign\EzMvc\System\Config::system('default-timezone', 'UTC'));
13+
14+
//Route request to controller action
1015
Alddesign\EzMvc\System\Router::routeRequest();

system/Controller.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@
77
*/
88
abstract class Controller
99
{
10-
protected static function isController()
10+
public static function isController()
11+
{
12+
return true;
13+
}
14+
15+
/**
16+
* Can be overridden in the App´s controller classes. Will be executed before the controller action is called.
17+
*
18+
* @param string $action Name of the actions based on the url controller/action
19+
* @param string $id First urls parameter after controller/action
20+
* @param array $params All further paramters after controller/action/id
21+
*
22+
* @return mixed If FALSE is returned, the request will be aborted (without any error)
23+
*/
24+
public static function onRequest(string $action, string $id, array $params)
1125
{
1226
return true;
1327
}

system/Helper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,15 @@ public static function session(string $name, &$outVar, $default = "")
303303
return true;
304304
}
305305

306+
/**
307+
* Returns the value of a session variable.
308+
* @return mixed|bool FALSE if the session variable doesnt exist
309+
*/
310+
public static function sessionVal(string $name, $default = false)
311+
{
312+
return isset($_SESSION[$name]) ? $_SESSION[$name] : $default;
313+
}
314+
306315
/**
307316
* Redirects to given url.
308317
*

system/Router.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,23 @@ public static function routeRequest()
3131
Helper::ex('Invalid URL "%s".', self::$originalRequestUrl);
3232
}
3333

34-
if(!method_exists(self::CONTROLLER_NAMESPACE . self::$controller, "isController"))
34+
if(!method_exists(self::CONTROLLER_NAMESPACE . self::$controller, 'isController'))
3535
{
3636
Helper::ex('Invalid controller "%s".', self::$controller);
3737
}
3838

39-
$result = call_user_func(sprintf('%s%s::%s', self::CONTROLLER_NAMESPACE, self::$controller, self::$action), self::$id, self::$params); //Redirect to controller action
40-
$restype = gettype($result);
39+
//onRequest
40+
$result = call_user_func([self::CONTROLLER_NAMESPACE . self::$controller, 'onRequest'], self::$action, self::$id, self::$params);
41+
if($result === false)
42+
{
43+
return;
44+
}
45+
46+
//call to controller action method
47+
$result = call_user_func([self::CONTROLLER_NAMESPACE . self::$controller, self::$action], self::$id, self::$params); //Redirect to controller action
48+
$resultType = gettype($result);
4149

42-
if($restype === 'string' || $restype === 'double' || $restype === 'integer' || $restype === 'boolean')
50+
if($resultType === 'string' || $resultType === 'double' || $resultType === 'integer' || $resultType === 'boolean')
4351
{
4452
echo $result;
4553
}

system/View.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class View
2020
/** @var array */
2121
public $data = [];
2222

23+
/** @var array Data which is shared across all views. Will be overridden if sames keys are used in regular data arrays. */
24+
public static $sharedData = [];
25+
2326
private const VIEWPATH = __DIR__."/../app/views/";
2427

2528
/**
@@ -46,7 +49,7 @@ private function __construct(string $name, array $data = [])
4649
}
4750

4851
$this->name = $name;
49-
$this->data = $data;
52+
$this->data = Helper::e(View::$sharedData)? $data : array_merge(View::$sharedData, $data);
5053
}
5154

5255
/**

system/system.config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
*/
88
$GLOBALS["_EZMVC_SYS_CONFIG"] =
99
[
10-
"app-name" => "Sample App", //The name of your app
10+
"app-name" => "EZ MVC Sample App", //The name of your app
1111
"base-url" => "http://localhost/ez-mvc", //Them url pointing to the root directory of EZ-MVC (where the index.php is located)
1212
"default-controller" => "Main", //The default controller when none is specified
1313
"default-action" => "index", //The default action when none is specified
14+
"default-timezone" => "Europe/Vienna", //See: https://www.php.net/manual/de/timezones.php
1415
"db-driver" => "sqlite", //"mysql", "sqlite" and "sqlsrv" are supported for now. https://www.php.net/manual/en/pdo.drivers.php
1516
"db-name" => dirname(__DIR__) . "/app/sample-database.sqlite",
1617
"db-host" => "",

0 commit comments

Comments
 (0)