Search by

erickfirmo / router

testerickfirmo

PHP Router

Package info

github.com/erickfirmo/php.router

pkg:composer/erickfirmo/router

Statistics

Installs: 158

Dependents: 1

Suggesters: 0

Stars: 9

Open Issues: 0

1.1 2022-05-04 17:38 UTC

This package is auto-updated.

Last update: 2026-10-29 18:59:41 UTC


README

PHP Router

PHP Router

A lightweight, object-oriented router for PHP applications.

Created by Erick Firmo.

GitHub language count Total Downloads Latest Stable Version License

Requirements

  • PHP 7.1 or higher
  • A web server configured to rewrite URLs

The router expects your web server to forward application requests to your index.php entry point.

Apache

On Apache, you can use mod_rewrite with a .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-l

    RewriteRule ^(.*)$ /index.php/$1
</IfModule>

Nginx

Nginx does not support .htaccess. Instead, URL rewriting must be configured in your server block.

For example:

location / {
    try_files $uri $uri/ /index.php/$uri;
}

Make sure PHP is properly configured to process your application's index.php file.

Installation

Install the package with Composer:

composer require erickfirmo/router

Usage

Create a router instance and load your application's routes:

<?php

// Load Composer's autoloader
require_once __DIR__ . '/vendor/autoload.php';

// Create a router instance
$router = new \ErickFirmo\Router;

// Configure the router (optional)

// Load the application's routes
require_once __DIR__ . '/routes/web.php';

// Handle the current request
$router->run();

Defining Routes

Routes can be defined using the HTTP methods supported by the router:

<?php

  $router->get('/examples', ExampleController::class, 'index', 'examples.index');

  $router->get('/examples/{id}', ExampleController::class, 'show', 'examples.show');

  $router->post('/examples/store', ExampleController::class, 'store', 'examples.store');

  $router->put('/examples/update/{id}', ExampleController::class, 'update', 'examples.update');

  $router->patch('/examples/update/{id}', ExampleController::class, 'update', 'examples.update');

  $router->delete('/examples/destroy/{id}', ExampleController::class, 'delete', 'examples.destroy');

A route can contain parameters using {parameter} syntax:

  $router->get('/examples/{id}', ExampleController::class 'show', 'examples.show');

The route parameter is passed to the corresponding controller method:

<?php

class ExampleController
{
    public function show(int $id)
    {
        echo $id;
    }
}

HTTP Method Spoofing

HTML forms only support GET and POST requests natively. To submit a form to a PUT, PATCH, or DELETE route, include a hidden _method field:

<input type="hidden" name="_method" value="PUT">

The router supports the following additional HTTP methods:

  • PUT
  • PATCH
  • DELETE

No additional configuration is required for GET or POST requests.

Configuration

The router provides several optional configuration methods.

Namespace

If your controllers share a common namespace, you can define a default namespace for the router:

<?php

$router->setNamespace('App\\Controllers\\');

This allows you to reference controllers without repeating the namespace in every route definition.

Custom 404 Page

If no route matches the incoming request, the router returns a default error message.

You can provide your own 404 view using the notFoundView() method:

<?php

$router->notFoundView(
    __DIR__ . '/../views/errors/404.php'
);

Passing Request Data

You can pass request data to the router using the setRequest() method:

<?php

$request = $_SERVER['REQUEST_METHOD'] === 'POST'
    ? $_POST
    : $_GET;

$router->setRequest('request', $request);

The value passed to setRequest() is injected as the first argument of the matched controller method.

For example:

<?php

class ExampleController
{
    public function myMethod(array $request, int $id)
    {
        echo $request['name'];
        echo $id;
    }
}

Objects can also be passed as request data:

<?php

class ExampleController
{
    public function myMethod(object $request, int $id)
    {
        echo $request->name;
        echo $id;
    }
}

License

This project is licensed under the MIT License.

See the LICENSE file for details.

Connect with me on LinkedIn   |   Visit my website