Coder Social home page Coder Social logo

fast-route-middleware's Introduction

Fast Route PSR 15 Middleware

Software License Scrutinizer Coverage Code Quality

A convenient and strict typed Fast Route middleware.

How to use it

Fast Routes result will be an array of which the first key represent the kind of result. Fast Route knows three different cases so far:

  1. Route found
  2. Route not found
  3. Route not allowed

The middleware deals with getting this result for you from Fast Route but you'll have to to define your handlers, because this is up to you and your application. The middleware takes an object that needs to implement an interface specific to each time.

You must create at least the found handler! Each kind of handler must implement the according interface. The other two handlers are optional!

// Route was found and matched the URL
class MyFoundHandler implements FoundHandlerInterface
{
    public function handle(ServerRequestInterface $request, $handler, array $vars): ?ResponseInterface
    {
        // Handle the request and return null or a response object
        // Dispatch your controllers or request handlers here based on the route vars
    }
}

// Route was not found, URL didn't match
class MyNotFoundHandler implements NotFoundHandlerInterface
{
    public function handle(ServerRequestInterface $request): ?ResponseInterface
    {
        // Handle the request and return null or a response object
        // Dispatch your controllers or request handlers here
    }
}

// Route was found but is not allowed to be accessible
class MyNotAllowedHandler implements NotAllowedHandlerInterface
{
    public function handle(ServerRequestInterface $request, array $notAllowedMethods): ?ResponseInterface
    {
        // Handle the request and return null or a response object
        // Dispatch your controllers or request handlers here
    }
}

Then configure the middleware. You must pass a FoundHandler the other two are optional!

Check the FastRoute documentation for how to configure FastRoutes dispatcher and it's routes.

$dispatcher = SimpleDispatcher(function(RouteCollector $r) { 
    // Your routes...
});

$fastRouteMiddleware = new FastRouteMiddleware(
    $dispatcher,
    new MyFoundHandler(),
    new MyNotFoundHandler(),
    new MyNotAllowedHandler()
);

// Pass the middleware to your middleware handler implementation

License

MIT License

Copyright (c) 2018 by Florian Krämer.

fast-route-middleware's People

Contributors

burzum avatar

Stargazers

 avatar  avatar

Watchers

 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.