A few days ago, I learned to build a basic router that maps URLs to controllers. Now, I need to enhance it to build a better router with advanced functionality. To build a better router that works with hidden inputs, first of all delete the basic router.php
file from project and create a new. Let's start.
Introduction to Routing
We need to build a better router that efficiently maps URLs to specific controllers or actions, allowing our application to handle requests and route them to the appropriate handlers.
The better router's ability to work with hidden inputs enables secure note deletion by passing the note ID to the controller without exposing it in the URL, preventing user interference.
<input type="hidden" name="_method" value="DELETE">
Create a Router File
To create a router file, we have to initialize the Router class with a namespace, in this case, Core
.
<?php
namespace Core;
class Router { }
Public Functions (Common Parameters)
As the router class is built then we have to define public functions in it and all have same parameters like get
, post
, delete
, patch
, and put
as essential routes that help our website determine what to do when someone visits a certain page. These functions have the same parameters, enabling them to perform the same actions.
public function get($uri, $controller) {
$this->add('GET', $uri, $controller); }
public function post($uri, $controller) {
$this->add('POST', $uri, $controller); }
public function delete($uri, $controller) {
$this->add('DELETE', $uri, $controller); }
public function patch($uri, $controller) {
$this->add('PATCH', $uri, $controller); }
public function put($uri, $controller) { $this->add('PUT', $uri, $controller); }
Add Method
As all public functions have same parameters then we use the add
method and give same parameters to it and only to call this in other functions. It is used to add a new route to the routing map, taking three parameters: the request method, the URI pattern to match, and the controller file to handle the request.
public function add($method, $uri, $controller) {
$this->routes[] = [
'uri' => $uri,
'controller' => $controller,
'method' => $method
];
}
Route Method
Here, We define the route
method to determine our application's response to a given URL, mapping it to the corresponding controller to handle the request.
public function route($uri, $method) {
foreach ($this->routes as $route) {
if ($route['uri'] === $uri && $route['method'] === strtoupper($method)) {
return require base_path($route['controller']);
}
}
$this->abort();
}
strtoupper Function
In route method, We use the strtoupper
function to convert a string to uppercase, ensuring a case-insensitive match.
strtoupper($method)
Protected Function (Abort)
In router.php file we defined the abort
method as a safety net, displaying an error page if our website cannot find the right route.
protected function abort($code = 404) {
http_response_code($code);
require base_path("views/{$code}.php");
die();
}
Route Definitions
Last thing is to define routing configuration in the routes.php
file, mapping URLs to corresponding controller actions.
$router->get('/', 'controllers/index.php');
$router->get('/about', 'controllers/about.php');
$router->get('/contact', 'controllers/contact.php');
$router->get('/notes', 'controllers/notes/index.php');
$router->get('/note', 'controllers/notes/show.php');
$router->get('/notes/create', 'controllers/notes/create.php');
The get method specifies the request method(GET) , the URL pattern and maps it to a controller file.
Conclusion
In conclusion, we have built a better router that efficiently maps URLs to specific controller methods, enabling a more structured and maintainable approach to handling requests and improving the overall performance and scalability of our website.
I hope that you have clearly understood it.
Top comments (1)