Creating a Better Router : Handling HIDDEN Inputs and DELETE request

PHPz
Release: 2024-07-17 00:44:00
Original
416 people have browsed it

Creating a Better Router : Handling HIDDEN Inputs and DELETE request

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">
Copy after login

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 { }
Copy after login

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); }
Copy after login

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 
  ]; 
}
Copy after login

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(); 
}
Copy after login

strtoupper Function

In route method, We use the strtoupper function to convert a string to uppercase, ensuring a case-insensitive match.

strtoupper($method)
Copy after login

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(); 
}
Copy after login

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');
Copy after login

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.

The above is the detailed content of Creating a Better Router : Handling HIDDEN Inputs and DELETE request. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!