Skip to content

patoui/router

Repository files navigation

⚠️ WARNING ⚠️

This repository is in active development, use at your own risk.

Router

Build Status Scrutinizer Code Quality

A simple HTTP router

Installation

Updated your composer.json file with the follow:

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/patoui/router"
        }
    ],
    "require": {
        "patoui/router": "dev-master"
    }
}

Then run the following command:

composer update patoui/router

Usage

<?php

require 'vendor/autoload.php';

use Patoui\Router\Route;
use Patoui\Router\RouteNotFoundException;
use Patoui\Router\Router;
use Patoui\Router\ServerRequest;

class HomeController
{
    public function index()
    {
        echo 'hello';
    }
}

$router = new Router();
$router->addRoute(new Route('get', '/foobar', HomeController::class, 'index'));

try {
    $resolvedRoute = $router->resolve(ServerRequest::makeWithGlobals());
    $controllerInstance = new ($resolvedRoute->getClassName());
    $controllerMethod = $resolvedRoute->getClassMethodName();
    $controllerMethodParameters = $resolvedRoute->getParameters();
    $controllerInstance->{$controllerMethod}(...$controllerMethodParameters);
} catch (RouteNotFoundException $notFoundException) {
    http_response_code(404);
} catch (\Exception $exception) {
    http_response_code(500);
}

Usage with PHP-DI

Add PHP-DI via composer

composer require php-di/php-di

Example

<?php

require 'vendor/autoload.php';

use DI\ContainerBuilder;
use Patoui\Router\Route;
use Patoui\Router\RouteNotFoundException;
use Patoui\Router\Router;
use Patoui\Router\ServerRequest;

$containerBuilder = new ContainerBuilder;
$container = $containerBuilder->build();

class Mailer
{
    public function mail($recipient, $content)
    {
        echo "Sent '{$content}' to '{$recipient}'";
    }
}

class UserManager
{
    private $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function register($email, $password)
    {
        $this->mailer->mail($email, 'Hello and welcome!');
    }
}

class HomeController
{
    public function __construct(UserManager $user_manager)
    {
        $this->user_manager = $user_manager;
    }

    public function show($id)
    {
        echo $id . PHP_EOL;
        $this->user_manager->register('[email protected]', 'foobar');
    }
}

$router = new Router();
$router->addRoute(new Route('get', '/foobar/{id}', HomeController::class, 'show'));

try {
    $resolvedRoute = $router->resolve(ServerRequest::makeWithGlobals());
    $container->call(
        [$resolvedRoute->getClassName(), $resolvedRoute->getClassMethodName()],
        $resolvedRoute->getParameters()
    );
} catch (RouteNotFoundException $notFoundException) {
    http_response_code(404);
} catch (Exception $exception) {
    http_response_code(500);
}

Testing

vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Fun attempt at a routing package

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages