Coder Social home page Coder Social logo

tiny-router's Introduction

tiny-router

Build Status Coverage Status

Tiny/Routing - it's a layer between the outside world like a browser or a console command CLI and your application. The package may be integrated to any existing php project and it does not require any extra packages.

Usually routing contains of two main parts:

  1. Routes - which describe a meta information like - a query, a query type, and a responsible controller which processes all incoming requests.

  2. A Router - just holds registered routes and matches incoming requests with registered routes.
    So it just returns either a matched route or trigger an Exception when a route is not found.

Current implementation of routing is very simple but in the same time very powerful. It gets you a possibility to work with it using a plain requests (literal) and Regexp based requests, also it supports filtering requests by http requests types like: GET, POST, etc.
Also you can use it as a routing for you CLI projects.

So let's check a look an http routing example:

    // create an instance of the router
    $router = new Router(new RequestHttpParams($_SERVER));

    // a literal `home` route
    $router->registerRoute(new Route(
        '/',
        'HomeController',
        'index'
    ));

    // a literal `users` route which accepts only `GET` and `POST` requests
    $router->registerRoute(new Route(
        '/users',
        'UserController',
        // list of actions
        [ 
            'GET' => 'list',
            'POST' => 'create'
        ]
    ));

    // a more complex example using a `RegExp` rule
    $router->registerRoute(new Route(
        '|^/users/(?P<id>\d+)$|i', // it's matches to: `/users/1`, `/users/300`, etc
        'UserController',
        [
            'GET' => 'view', 
            'DELETE' => 'delete',
        ],
        'regexp', 
        ['id']
    ));

    // now get a matched route 
    $matchedRoutes = $router->getMatchedRoute();

Installation

Run the following to install this library:

$ composer require esase/tiny-router

Documentation

https://tiny-docs.readthedocs.io/en/latest/tiny-router/docs/index.html

tiny-router's People

Contributors

esase avatar

Stargazers

 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.