The Router PHP package provides an easy-to-use routing system for PHP web applications. It allows developers to define routes for different HTTP methods such as GET, POST, DELETE, and PUT. Additionally, it supports the use of hidden input fields to simulate the DELETE, PUT, and PATCH HTTP methods.
The Router PHP package requires PHP 7.0 or higher.
- Define routes for GET, POST, DELETE, and PUT HTTP methods
- Define routes with parameters
To use the Router PHP package, you need to install it via Composer. Run the following command in your project directory:
composer require pierre/router
To use the Router, you need to create a new instance of the Router class:
// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';
// Create Router instance
$router = new \Pierre\Router\Router();
// Define routes
// ...
// Run it!
The Router supports four HTTP methods: GET, POST, DELETE, and PUT. You can define a route for each method using the following methods:
To define a GET route, use the get() method. The first parameter is the URL path, and the second parameter is a callback function that will be executed when the route is accessed:
$router->get('/', function() {
return 'Hello World!';
});
To define a POST route, use the post() method.
$router->post('/', function() {
return $_POST['name'];
});
To define a DELETE route, use the delete() method.
$router->delete('/user/:id', function($id) {
return 'User ' . $id . ' deleted';
});
To define a PUT route, use the put() method.
$router->put('/user/:id', function($id) {
return 'User ' . $id . ' updated';
});
After defining your routes, you need to execute the Router using the run() method:
$router->run();
To simulate the DELETE, PUT, and PATCH HTTP methods in a form, you need to include a hidden input field with the name _method and the value of the desired method. The Router will detect this input field and use the specified method for the request.
<form action="/user/delete/1" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="id" value="1">
<input type="submit" value="Delete">
</form>
<form action="/user/edit/1" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="id" value="1">
<input type="submit" value="Delete">
</form>
You can pass parameters to your routes using the following syntax:
$name = "Pierre";
$router->get('/hello', function() use ($name) {
return "Hello {$name}!}";
});
The Router PHP package is open-sourced software licensed under the MIT license.