websitesql/router

Router component for WebsiteSQL framework

v1.0.0 2025-04-15 09:11 UTC

This package is auto-updated.

Last update: 2025-04-15 09:57:30 UTC


README

A lightweight, flexible routing library for PHP applications that extends the functionality of League\Route and League\Container while adding a custom API strategy for consistent API responses.

Overview

WebsiteSQL Router is built on top of the following packages:

This library extends these components and provides a custom ApiStrategy to standardize API responses across your application.

Installation

Install via Composer:

composer require websitesql/router

Basic Usage

use WebsiteSQL\Router\Router;
use WebsiteSQL\Router\Container;
use WebsiteSQL\Router\Strategy\ApiStrategy;
use WebsiteSQL\Router\Factory\ResponseFactory;

// Create container & router
$router = new Router();
$container = new Container();

// Create the strategy
$responseFactory = new ResponseFactory();
$strategy = new ApiStrategy($responseFactory);
$strategy->setContainer($container);
$strategy->setCorsOptions([
	'origin' => ['https://example.com'],
	'methods' => ['GET', 'POST', 'PUT', 'DELETE'],
	'headers.allow' => ['Content-Type', 'Authorization'],
	'headers.expose' => ['Etag'],
	'credentials' => true,
	'cache' => 86400
]);
$router->setStrategy($strategy);

// Define routes
$router->get('/users', 'UserController::listUsers');
$router->post('/users', 'UserController::createUser');

// Dispatch the request
$response = $router->dispatch($request);

Features

Extended Router

The Router class extends League\Route's Router with additional functionality:

  • Simplified API route definitions
  • Automatic content negotiation
  • Configurable middleware application
  • Enhanced error handling

Extended Container

The Container class extends League\Container to provide:

  • Automatic controller resolution
  • Simplified service registration
  • Integration with router components

Custom ApiStrategy

The ApiStrategy standardizes how API responses are formatted:

// Example controller using ApiStrategy
public function getUser(ServerRequestInterface $request): array
{
    $userId = $request->getAttribute('id');
    $user = $this->userRepository->find($userId);
    
    // Return data as array - ApiStrategy will convert to proper response
    return ['data' => $user];
}

Benefits of the ApiStrategy:

  • Consistent JSON response structure
  • Automatic status code handling
  • Standard error formatting
  • Content negotiation
  • CORS support

Configuration

Customizing ApiStrategy

use WebsiteSQL\Router\Strategy\ApiStrategy;

$strategy = new ApiStrategy();
$strategy->setContainer($container);

// Configure CORS 
$strategy->setCorsOptions([
    'origin' => ['https://example.com'],
    'methods' => ['GET', 'POST', 'PUT', 'DELETE'],
    'headers.allow' => ['Content-Type', 'Authorization'],
    'headers.expose' => ['Etag'],
    'credentials' => true,
    'cache' => 86400
]);

$router->setStrategy($strategy);

Error Handling

The ApiStrategy automatically converts exceptions to appropriate HTTP responses:

  • NotFoundException → 404 Not Found response
  • UnauthorizedException → 401 Unauthorized response
  • ForbiddenException → 403 Forbidden response
  • Other exceptions → 500 Internal Server Error response

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This package is open-sourced software licensed under the MIT license.