PHP Library to create HTTP routes
- PHP >= 8.0
installation using composer
composer require albertoadolfo27/friendly_route
<?php
// Require the Composer autoloader.
require_once "vendor/autoload.php";
use FriendlyRoute\Router;
// Set the configuration.
$config = array(
"debug" => true,
"projectDir" => "friendly_route" // Set the Project Directory if you are working on the localhost. Default empty string.
);
// Instantiate a Router.
$router = new Router($config);
// Tracing routes
$router->get("/", function () {
echo "<h1>Hello World!</h1>";
});
$router->get("/user/{username}", function ($args) {
$user = $args["username"];
echo "<h1>Welcome {$user}</h1>";
});
$router->notFound(function () {
echo "<h1>404 NOT FOUND</h1>";
});
$router->run();
- GET
$router->get("/", function () {
// Put the code to run
});
or
$router->set("GET", "/", function () {
// Put the code to run
});
- POST
$router->post("/", function () {
// Put the code to run
});
or
$router->set("POST", "/", function () {
// Put the code to run
});
- PUT
$router->put("/", function () {
// Put the code to run
});
or
$router->set("PUT", "/", function () {
// Put the code to run
});
- DELETE
$router->delete("/", function () {
// Put the code to run
});
or
$router->set("DELETE", "/", function () {
// Put the code to run
});
$router->set(array("GET", "POST"), "/", function () {
// Put the code to run
});
$router->get(["/hello","/hi"], function () {
// Put the code to run
});
$router->methodNotAllowed(function () {
echo "<h1>405 METHOD NOT ALLOWED</h1>";
});