albertoadolfo27/friendly_route

PHP Library to create HTTP routes

Maintainers

Package info

github.com/AlbertoAdolfo27/friendly_route

pkg:composer/albertoadolfo27/friendly_route

Statistics

Installs: 20

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.1.0 2025-08-14 12:47 UTC

This package is auto-updated.

Last update: 2026-03-14 14:05:48 UTC


README

PHP Library to create HTTP routes

Getting started

Minimum requirements

  • PHP >= 8.0

Installation

installation using composer

composer require albertoadolfo27/friendly_route

Quick Examples

Tracing routes

<?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();

Allowed HTTP methods

  • 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
});

Plotting a route with more than one HTTP method

$router->set(array("GET", "POST"), "/", function () {
    // Put the code to run
});

Add array of routes to a request

$router->get(["/hello","/hi"], function () {
    // Put the code to run
});

Method not allowed

$router->methodNotAllowed(function () {
    echo "<h1>405 METHOD NOT ALLOWED</h1>";
});