Skip to content

Commit b2884a2

Browse files
committed
v1.0.0-beta.5
1 parent 902c20d commit b2884a2

File tree

5 files changed

+104
-27
lines changed

5 files changed

+104
-27
lines changed

config/config.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
define('EZ_APP_NAME', 'EZ MVC Sample App'); //The name of your app
99
define('EZ_BASE_URL', 'http://localhost/ez-mvc/'); //The url pointing to the root directory of EZ-MVC (where the index.php is located)
10-
define('EZ_DEFAULT_CONTROLLER', 'Main'); //The default controller (class name) when none is specified
11-
define('EZ_DEFAULT_ACTION', 'index'); //The default action (method name in the controller class) when none is specified
10+
define('EZ_DEFAULT_URL', '/Main/index'); //A request to the BASE_URL directly resolves to this Url (relative)
1211
define('EZ_DEFAULT_TIMEZONE', 'Europe/Vienna'); //PHP Timezone. See: https://www.php.net/manual/de/timezones.php
1312
define('EZ_DB_DRIVER', 'sqlite');//The DB driver: 'mysql', 'sqlite' and 'sqlsrv' are supported for now. See: https://www.php.net/manual/en/pdo.drivers.php
1413
define('EZ_DB_NAME', dirname(__DIR__) . '/app/sample-database.sqlite'); //The name of the database or path to the file in case of sqlite
@@ -21,4 +20,4 @@
2120
define('EZ_PHP_DISPLAY_ERRORS', 'On'); //'On' or 'Off'. Specify if PHP shows runtime errors at all. See: https://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors
2221
define('EZ_PHP_DISPLAY_STARTUP_ERRORS', 'On'); //'On' or 'Off'. Specify if PHP shows startup errors. See: https://www.php.net/manual/en/errorfunc.configuration.php#ini.display-startup-errors
2322
define('EZ_PHP_ERROR_REPORTING', -1); //PHP error reporting level. See: https://www.php.net/manual/en/function.error-reporting.php
24-
define('EZ_VERSION', 'v1.0.0'); //Version of ez-mvc (just in case u wanna know)
23+
define('EZ_VERSION', 'v1.0.0-beta.5'); //Version of ez-mvc (just in case u wanna know)

system/Helper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
declare(strict_types = 1);
33
namespace Alddesign\EzMvc\System;
44

5+
require_once(__DIR__ . '/HelperGlobal.php');
6+
57
abstract class Helper
68
{
79
/**
@@ -305,4 +307,5 @@ public static function redirect(string $url, bool $literalUrl = false)
305307
header('Location: ' . $url);
306308
die();
307309
}
308-
}
310+
}
311+

system/HelperGlobal.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
declare(strict_types = 1);
3+
/**
4+
* No Namespace
5+
* Defining some shorthand functions in gloabal scope.s
6+
*/
7+
8+
/**
9+
* Shorthand for \Alddesign\EzMvc\System\Helper::url($url)
10+
* @return string
11+
*/
12+
function u(string $url)
13+
{
14+
return \Alddesign\EzMvc\System\Helper::url($url);
15+
}

system/NotFound.html.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types = 1);
3+
namespace Alddesign\EzMvc\System;
4+
?>
5+
<!DOCTYPE html>
6+
<html lang="en">
7+
<head>
8+
<title><?= htmlspecialchars($title) ?></title>
9+
</head>
10+
<body>
11+
<div style="text-align: center; font-family:'Courier New', Courier, monospace;">
12+
<h1><?= htmlspecialchars($title) ?></h1>
13+
<hr/>
14+
<p><?= htmlspecialchars($message) ?></p>
15+
</div>
16+
</body>
17+
</html>

system/Router.php

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
*/
1010
abstract class Router
1111
{
12-
private static $originalRequestUrl = '';
1312
private static $controller = '';
1413
private static $action = '';
1514
private static $id = '';
1615
private static $params = [];
16+
17+
public static $defaultUrlApplied = false;
18+
public static $resolvedPath = '';
19+
1720
private const CONTROLLER_NAMESPACE = 'Alddesign\\EzMvc\\Controllers\\';
1821

1922

@@ -24,18 +27,32 @@ abstract class Router
2427
*/
2528
public static function routeRequest()
2629
{
27-
self::resolveRequestUrl();
30+
self::resolveRequestUrl($_SERVER['REQUEST_URI']);
2831

29-
if(!method_exists(self::CONTROLLER_NAMESPACE . self::$controller, self::$action))
32+
//Validate
33+
$controllerExists = class_exists(self::CONTROLLER_NAMESPACE . self::$controller);
34+
if(!$controllerExists)
3035
{
31-
Helper::ex('Invalid URL "%s".', self::$originalRequestUrl);
36+
self::returnError(404, 'Not Found', sprintf('The requested URL "%s" was not found.', $_SERVER['REQUEST_URI']));
37+
}
38+
$isController = method_exists(self::CONTROLLER_NAMESPACE . self::$controller, 'onRequest');
39+
if(!$isController)
40+
{
41+
self::returnError(500, 'Invalid Controller', sprintf('Class "%s" is not a valid controller.', self::$controller));
3242
}
3343

34-
if(!method_exists(self::CONTROLLER_NAMESPACE . self::$controller, 'isController'))
44+
$actionExists = method_exists(self::CONTROLLER_NAMESPACE . self::$controller, self::$action);
45+
if(!$actionExists)
3546
{
36-
Helper::ex('Invalid controller "%s".', self::$controller);
47+
self::returnError(500, 'Invalid Action', sprintf('Action "%s" not found in controller class "%s".', self::$action, self::$controller));
3748
}
3849

50+
//Call controller & action
51+
self::callControllerAction();
52+
}
53+
54+
private static function callControllerAction()
55+
{
3956
//onRequest
4057
$result = call_user_func([self::CONTROLLER_NAMESPACE . self::$controller, 'onRequest'], self::$action, self::$id, self::$params);
4158
if($result === false)
@@ -45,8 +62,8 @@ public static function routeRequest()
4562

4663
//call to controller action method
4764
$result = call_user_func([self::CONTROLLER_NAMESPACE . self::$controller, self::$action], self::$id, self::$params); //Redirect to controller action
48-
49-
if(in_array(gettype($result), ['string','double','integer','boolean']))
65+
$resultType = gettype($result);
66+
if(in_array($resultType, ['string','double','integer','boolean']))
5067
{
5168
echo $result;
5269
}
@@ -57,26 +74,52 @@ public static function routeRequest()
5774
*
5875
* @return void
5976
*/
60-
private static function resolveRequestUrl()
61-
{
62-
//Request Url:
63-
$url = parse_url(Helper::addTrailingSlash('http://example.com' . urldecode($_SERVER['REQUEST_URI'])));
64-
$urlPath = isset($url['path']) ? $url['path'] : '/';
65-
self::$originalRequestUrl = $urlPath;
77+
private static function resolveRequestUrl(string $requestUri)
78+
{
79+
//Prepare request url
80+
$reqUrl = Helper::addStartingSlash($requestUri);
81+
$reqUrl = urldecode($reqUrl);
82+
$reqUrlPath = self::extractUrlPath('http://request.url' . $reqUrl);
6683

6784
//Base Url:
68-
$baseUrl = parse_url(Helper::addTrailingSlash(EZ_BASE_URL));
69-
$baseUrlPath = isset($baseUrl['path']) ? $baseUrl['path'] : '/';
85+
$baseUrl = Helper::addTrailingSlash(EZ_BASE_URL);
86+
$baseUrlPath = self::extractUrlPath($baseUrl);
7087

7188
//Strip base url path from request url path:
72-
$urlPath = mb_strpos($urlPath, $baseUrlPath, 0) === 0 ? '/' . mb_substr($urlPath, mb_strlen($baseUrlPath)) : $urlPath;
89+
$path = mb_strpos($reqUrlPath, $baseUrlPath) === 0 ? '/' . mb_substr($reqUrlPath, mb_strlen($baseUrlPath)) : $reqUrlPath;
90+
91+
//Default URL
92+
if($path === '/')
93+
{
94+
self::$defaultUrlApplied = true;
95+
$path = Helper::addStartingSlash(EZ_DEFAULT_URL);
96+
}
97+
98+
//Split path into Controller, Action and params
99+
$pathParts = explode('/', $path);
100+
self::$resolvedPath = $path;
101+
self::$controller = isset($pathParts[1]) && $pathParts[1] !== '' ? $pathParts[1] : '';
102+
self::$action = isset($pathParts[2]) && $pathParts[2] !== '' ? $pathParts[2] : '';
103+
self::$id = isset($pathParts[3]) ? $pathParts[3] : '';
104+
self::$params = isset($pathParts[4]) ? array_slice($pathParts, 4) : [];
73105

74-
//Split request url path:
75-
$urlPathParts = explode('/', $urlPath);
106+
return;
107+
}
76108

77-
self::$controller = isset($urlPathParts[1]) && $urlPathParts[1] !== '' ? $urlPathParts[1] : EZ_DEFAULT_CONTROLLER;
78-
self::$action = isset($urlPathParts[2]) && $urlPathParts[2] !== '' ? $urlPathParts[2] : EZ_DEFAULT_ACTION;
79-
self::$id = isset($urlPathParts[3]) ? $urlPathParts[3] : '';
80-
self::$params = isset($urlPathParts[4]) ? array_slice($urlPathParts, 4) : [];
109+
/** @return string */
110+
private static function extractUrlPath(string $url)
111+
{
112+
$parts = parse_url($url);
113+
return $parts !== FALSE ? ($parts['path'] ?? '/') : '/';
114+
}
115+
116+
/**
117+
* Calls NotFound.html.php with specified http response code, title and message and end the request.
118+
*/
119+
private static function returnError(int $httpResponseCode, string $title, string $message)
120+
{
121+
http_response_code($httpResponseCode);
122+
require(__DIR__ . '/NotFound.html.php');
123+
die;
81124
}
82125
}

0 commit comments

Comments
 (0)