Giter Club home page Giter Club logo

ldl-http-router's Introduction

#LDL Http Router

A router based on phroute (for now)

LDL Http Router is a type of URL router which uses a chain of responsibility for pre, main, and post dispatchers.

Single "controller" (Dispatcher)

Routes consist of a main dispatcher class, which (ideally) contains a single dispatch method (plus needed dependencies injected via __construct or other methods).

By having just a "single controller" class with the dispatch method, all other logic such as user validation or other pre conditions can be simplified in a single pre dispatcher and reused among other routes.

Response parsers

Route dispatchers return data, just like any regular PHP method, and they are not allowed to modify the response body. For that, route dispatchers exist, which basically take in all the data from a request (pre, main and post dispatchers) and transform it to a string representation.

By default, this router focuses on REST type responses and thus, the results from the entire route will be JSON encoded, however other types of responses can be provided, for example using straight HTML.

Please see the Template response parser plugin if you need any kind of templating, this plugin makes it possible to use any kind of templating engine you desire.

Pre and post dispatchers

These are actions that come before (pre) or after (post) the main dispatcher execution.

Pre and post dispatchers can be specified at:

  • Router level (all routes will apply said pre and post dispatchers)
  • Route level (only one or more routes implement some pre and post dispatch handlers)

All pre and post dispatchers are part of a middleware chain collection, each element of this collection has:

  • A namespace and a name, with this in mind, no dispatchers compete with other dispatchers in the HTTP Response
  • A priority, which establishes the order of execution of the middleware chain
  • An active flag, to enable or disable a dispatcher

Exception handlers

Exception handlers are used when an exception is thrown from any type of dispatcher (be it pre, main or post) with exception handlers the developer can act on certain types of exceptions thrown and be able to provide a proper HTTP Response code, plus doing other actions, such as error logging, etc.

If an exception for which no exception handler exists is thrown, then the exception will simply throw a fatal error as expected.

Exception handlers can also be applied at:

  • A router level (for example, when a route is not found)
  • A route level (for example, when you want to handle a custom exception on a single route)

Route configuration file

You can add any route you like to the router object manually, however this is discouraged. For this purpose you can add a routes.json configuration file with your own route definitions.

Check this example to know how to define a route in a json configuration file.

Useful plugins

Examples

Make sure to check the examples at the example folder

TODO

  • Add other config file parsers such as YML or XML
  • Add more documentation regarding routes.json

Bugs

If you'd like to report a bug, please open an issue here at our github page

ldl-http-router's People

Contributors

pthreat avatar gonzalo-coda avatar

Watchers

James Cloos 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.