Coder Social home page Coder Social logo

router's Introduction

Synopsis

A simple and fast router, nothing more. The tree-building mechanism is used to quickly find the route.

Code Example

$switcher = new \vivace\router\Switcher([
    //To check the URL path in a template, the rule must begin with a slash, for example /foo/bar
    '/hello/:message' => function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) {
        return 'Hello ' . $request->getAttribute('message');
    },
    '/' => function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) {
        return 'Index Page';
    },
    'POST/user' => function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) {
        return 'Create user';
    },
    'GET//:app.mysite.com/user/:id ' => function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) {
        $app = $request->getAttribute('app');
        $id = $request->getAttribute('id');
        return "Request to $app for getting user by id=$id";
    },
    //domain rules should started with double slashes `//`
    '//admin.mysite.com' => function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) {
        return 'Admin page';
    },
    //It supports nested in one another
    '/api' => new \vivace\router\Switcher([
        '/v1' => function () {
            return 'Nested switcher';
        },
        '/v2' => new \vivace\router\Switcher([
            '/' => function () {
                return 'Deep nested switcher';
            }
        ])
    ]),
    'OPTIONS' => function(){
        return 'Request with OPTIONS method.';
    }
]);
//$request and $response is objects, which implementation of psr-7, this library not contain implementation psr-7, you should use third-party libraries, which do it.
$response = $switcher->switch($request, $response);
echo $response->getBody()->getContents();

Motivation

The goal was to create a fast router with support for routing over the host, capturing variables, embedding. Compatibility with PSR-7 was also required.

Installation

php composer.phar require vivace/router

API Reference

Switcher::case(string $pattern, callable $handler)

Add handler, which will called if request matched by pattern successfully.

Switcher::switch(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface

Find and call handler for request

Switcher::default(callable $handler)

Add default handler, for case when route not matched.

Tests

Via local installed php

phpunit --testsuite=unit --coverage-html=./tests/coverage/

Via docker-compose

docker-compose run --rm phpunit --testsuite=unit --coverage-html=./tests/coverage/

Contributors

Albert Sultanov [email protected]

License

Copyright (c) 2017 Albert Sultanov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

router's People

Contributors

typomaker avatar

Stargazers

 avatar

Watchers

 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.