From 20af342a164b6a34520c32f0ebb18b31a02024ab Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 10 Sep 2024 19:02:34 +0800 Subject: [PATCH 1/2] route method regex explained --- session-07/S07-Vanilla-PHP-MVC-Pt-03.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/session-07/S07-Vanilla-PHP-MVC-Pt-03.md b/session-07/S07-Vanilla-PHP-MVC-Pt-03.md index f34ebf5..9f7d73e 100644 --- a/session-07/S07-Vanilla-PHP-MVC-Pt-03.md +++ b/session-07/S07-Vanilla-PHP-MVC-Pt-03.md @@ -423,7 +423,12 @@ if (count($uriSegments) === count($routeSegments) && strtoupper($route['method'] $match = false; break; } - + // /is the start and end of regex pattern + // \escapes the curly brace, making it a regular character, it's looking for it. + // parenthesis is the capturing group + // . is any character that is not a newline + // + is one or more proceeding elements + // ? lazy making match as few characters as possible if (preg_match('/\{(.+?)}/', $routeSegments[$i], $matches)) { $params[$matches[1]] = $uriSegments[$i]; } From 7d852fa5f947092ac09da14244d3b8bea0271316 Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 10 Sep 2024 19:44:50 +0800 Subject: [PATCH 2/2] start session 3:27:00 --- session-07/S07-Vanilla-PHP-MVC-Pt-03.md | 103 +++++++++++++----------- 1 file changed, 57 insertions(+), 46 deletions(-) diff --git a/session-07/S07-Vanilla-PHP-MVC-Pt-03.md b/session-07/S07-Vanilla-PHP-MVC-Pt-03.md index 9f7d73e..58fd096 100644 --- a/session-07/S07-Vanilla-PHP-MVC-Pt-03.md +++ b/session-07/S07-Vanilla-PHP-MVC-Pt-03.md @@ -372,13 +372,15 @@ Yep, a lot is done by this one method. Here are the parts of the method with quick descriptions... ```php -$requestMethod = $_SERVER['REQUEST_METHOD']; - -// Check for _method input -if ($requestMethod === 'POST' && isset($_POST['_method'])) { - // Override the request method with the value of _method - $requestMethod = strtoupper($_POST['_method']); -} +public function route($uri) +{ + $requestMethod = $_SERVER['REQUEST_METHOD']; + + // Check for _method input + if ($requestMethod === 'POST' && isset($_POST['_method'])) { + // Override the request method with the value of _method + $requestMethod = strtoupper($_POST['_method']); + } ``` Here we get the request (HTTP) method that is being used (GET, POST). @@ -399,39 +401,43 @@ foreach ($this->routes as $route) { $routeSegments = explode('/', trim($route['uri'], '/')); $match = true; -``` -Next we need to go through the registered routes one by one. + // Constructor for Database class + // Next we need to go through the registered routes one by one. -We explode the incoming request URI into segments. This is ready for matching. + // We explode the incoming request URI into segments. This is ready for matching. -We do the same for the URI of the current registered route. + // We do the same for the URI of the current registered route. -We then set a default value for match completion, so we only need to change this when no match is found. + // We then set a default value for match completion, so we only need to change this when no match is found. -The next part is where we check the segments of the request against the segments of the registered route... - -```php -if (count($uriSegments) === count($routeSegments) && strtoupper($route['method'] === $requestMethod)) { - $params = []; + // The next part is where we check the segments of the request against the segments of the registered route... + + if (count($uriSegments) === count($routeSegments) + && strtoupper($route['method'] === $requestMethod)) { + $params = []; - $match = true; - $segments = count($uriSegments); - for ($i = 0; $i < $segments; $i++) { - - if ($routeSegments[$i] !== $uriSegments[$i] && !preg_match('/\{(.+?)}/', $routeSegments[$i])) { - $match = false; - break; - } - // /is the start and end of regex pattern - // \escapes the curly brace, making it a regular character, it's looking for it. - // parenthesis is the capturing group - // . is any character that is not a newline - // + is one or more proceeding elements - // ? lazy making match as few characters as possible - if (preg_match('/\{(.+?)}/', $routeSegments[$i], $matches)) { - $params[$matches[1]] = $uriSegments[$i]; - } + $match = true; + $segments = count($uriSegments); + + for ($i = 0; $i < $segments; $i++) { + // If the uri's do not match and there is no parm + if ($routeSegments[$i] !== $uriSegments[$i] + // /is the start and end of regex pattern + // \escapes the curly brace, making it a regular character, it's looking for it. + // parenthesis is the capturing group + // . is any character that is not a newline + // + is one or more proceeding elements + // ? lazy making match as few of the preceding characters as possible + // eg /products/{id} + && !preg_match('/\{(.+?)}/', $routeSegments[$i])) { + $match = false; + break; + } + + if (preg_match('/\{(.+?)}/', $routeSegments[$i], $matches)) { + $params[$matches[1]] = $uriSegments[$i]; + } } ``` @@ -449,19 +455,23 @@ If the previous decision is false we then check and add the parameter to the par We are now ready to process the middleware component of the route, if required... ```php -if ($match) { - foreach ($route['Middleware'] as $middleware) { - (new Authorise())->handle($middleware); - } - - $controller = 'App\\controllers\\' . $route['controller']; - $controllerMethod = $route['controllerMethod']; - - // Instantiate the controller and call the method - $controllerInstance = new $controller(); - $controllerInstance->$controllerMethod($params); - return; + if ($match) { + foreach ($route['Middleware'] as $middleware) { + (new Authorise())->handle($middleware); + } + + $controller = 'App\\controllers\\' . $route['controller']; + $controllerMethod = $route['controllerMethod']; + + // Instantiate the controller and call the method + $controllerInstance = new $controller(); + $controllerInstance->$controllerMethod($params); + return; + } + } } + +ErrorController::notFound(); ``` If we get to this block of code and the `match` is still true we are able to process the middleware requirements. @@ -483,6 +493,7 @@ You are expected to update the header comments to give a title and explain the p ## Session +3:27:00 Use the same technique as previously to create a new class in the Framework folder, this time called Session. Fill out the header DocBlock as required.