lumax/routing-component

A dynamic PHP Routing Component.

1.4.2 2024-04-17 21:57 UTC

This package is auto-updated.

Last update: 2024-04-17 21:58:09 UTC


README

Version 1.4.2 PHP Coverage 97.27% License GPL--3.0--or--later

The Routing Component is a lightweight and flexible PHP package for handling routing in your web application. It provides a simple way to define routes and execute controller actions based on incoming HTTP requests. This component is designed to be easily integrated into your PHP projects and works seamlessly with PSR-11 compliant dependency injection containers.

Installation

You can install this package using Composer:

composer require lumax/routing-component

Usage

Basic Usage

Create a Router Instance

Create an instance of the Router class, passing a dependency injection container that implements the Psr\Container\ContainerInterface:

use Luma\RoutingComponent\Router;
use Psr\Container\ContainerInterface;

$router = new Router($container);

Load Routes:

Load your application's routes from a YAML configuration file:

$router->loadRoutesFromFile('routes.yaml');

Example YAML configuration (routes.yaml):

routes:
  index:
    path: /
    handler: [App\Controllers\HomeController, index]
  user_index:
    path: /user/{id}
    handler: [App\Controllers\UserController, show]

Alternatively, you can load your routes from an array if you'd prefer:

$routes = [
    [
        'path' => '/',
        'handler' => [
            'App\\Controllers\\HomeController',
            'index',
        ]       
    ],
    [
        'path' => '/user/{id}',
        'handler' => [
            'App\\Controllers\\UserController',
            'show',
        ]       
    ],
];

$router->loadRoutes($routes);

Handle Requests:

In your application's entry point (e.g., index.php), call the handleRequest method to handle incoming HTTP requests:

$router->handleRequest($request);

The handleRequest method expects an instance of Psr\Http\Message\RequestInterface. This Routing Component requires my HTTP Component, therefore Request and Response classes are already provided.

The router will match the request URI to the defined routes and execute the corresponding controller action.

Controller Actions

Controller actions are defined as arrays containing the controller class name and the method name:

['App\Controllers\HomeController', 'index']

Dependencies

The Router class is designed to work seamlessly with dependency injection containers. You can inject dependencies into your controller actions through constructor injection. When a controller is instantiated, the router will automatically resolve and inject its dependencies from the container. Requires a PSR-11 compliant ContainerInterface instance.

Error Handling

The router handles 404 Not Found errors for unhandled routes. If no matching route is found, it will respond with a 404 status code.

License

This package is open-source software licensed under the GNU General Public License, version 3.0 (GPL-3.0).