Git Product home page Git Product logo

sfw-router's Introduction

SFW Router

Build Status codecov Maintainability Test Coverage License: MIT

A simple Router that maps incoming requests to predefined handlers.

It builds a tree using segments of a predefined uri as internal nodes and its corresponding handler as the terminal node. It resolves routes by traversing this tree.

Wildcard segments are supported and captured for use in the handler. See usage for more detail.

Exact segment match wins over wildcard match. Eg. if you have /foo/bar and /foo/{x} defined as routes with corresponding handlers Bar and X then /foo/bar will be handled by Bar while /foo/baz will be handled by X.

Table of Contents

Requirements

PHP 7.1+

Installation

composer require pwm/sfw-router

Usage

// Router depends on Request
use SFW\Request\Request;
use SFW\Request\RequestMethod as Method;
use SFW\Request\RequestUri as Uri;

// Have some controllers
class FooCtrl
{
    public function getAll(Request $request): array { /* ... */ }
    public function post(Request $request): bool { /* ... */ }
}
class BarCtrl
{
    public function getById(Request $request, $fooId, $barId): Bar { /* ... */ }
}

// Create router
$router = new Router();

// Add routes and corresponding route handlers
$router->add(new Route(new Method(Method::GET), new Uri('/foo')), new RouteHandler(FooCtrl::class, 'getAll'));
$router->add(new Route(new Method(Method::POST), new Uri('/foo')), new RouteHandler(FooCtrl::class, 'post'));
$router->add(new Route(new Method(Method::GET), new Uri('/foo/{id}/bar/{id}')), new RouteHandler(BarCtrl::class, 'getById'));

// Resolve a handler for an incoming request
$routeHandler = $router->resolve(new Route($request->getMethod(), $request->getUri()));

// (Optional) Resolve the handler class from the container and call the handling method 
$response = $container
    ->resolve($routeHandler->getClassName())
    ->{$routeHandler->getMethodName()}($request, ...$routeHandler->getRoute()->getCapturedSegments());

How it works

TBD

Tests

$ vendor/bin/phpunit
$ composer phpcs
$ composer phpstan

Changelog

Click here

Licence

MIT

sfw-router's People

Contributors

pwm avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.