2017 © Pedro Peláez
 

library epicroute

Very fast request router

image

kito92/epicroute

Very fast request router

  • Tuesday, March 7, 2017
  • by Kito92
  • Repository
  • 1 Watchers
  • 4 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

EpicRoute - A very fast router for PHP

With this library you can easily implement routing in your PHP application, and it's insanely fast!, (*1)

Installation

To install with composer:, (*2)

composer require kito92/epicroute

Requires PHP 5.3+., (*3)

Basic Usage

A simple file setup is located under 'example' directory. Here's a basic usage example:, (*4)

<?php
require 'vendor/autoload.php';
$route = \EpicRoute\Route::getInstance();

$route->get('/', function(){
    echo 'response';
});

$route->dispatch();

Available methods are the following:, (*5)

$route->get('/...', function(){ /*...*/ });
$route->post('/...', function(){ /*...*/ });
$route->put('/...', function(){ /*...*/ });
$route->patch('/...', function(){ /*...*/ });
$route->delete('/...', function(){ /*...*/ });

Variables & Regex

Here's how you can match a variable:, (*6)

$route->get('/users/:name', function($name){
    echo 'Welcome back ' . $name;
});

and in this way you can match only numeric value:, (*7)

$route->get('/users/:id{[0-9]*}', function($id){
    echo 'user id ' . $id;
});

To match all the sub-URLs you can add the "+" at the end of a variable's name:, (*8)

$route->get('/:slug+', function($paths){
    print_r($paths);
});

This will match /any/route/you/goes, and the variable $paths is an array which contains the subfolder requested., (*9)

Remember that, for the same base url, if you have multiple regex route and a generic one, you HAVE to declare first those who have a regex, than the generic one, or the router will serve always the generic. You can call several methods through the fluent interface. So, for example:, (*10)

$route->get('/users/:id{[0-9]*}', function($id){ /*...*/ })
      ->get('/users/:name', function($name){ /*...*/ })
      ->dispatch();

Middleware

You can define your custom middleware implementing the \EpicRoute\Middleware interface:, (*11)

class CustomMiddleware implements \EpicRoute\Middleware{
    /**
     * This method will be executed before the routing
     *
     * @return mixed
     */
    function before(){
        // TODO: Implement before() method.
    }

    /**
     * This method will be executed after the routing
     *
     * @return mixed
     */
    function after(){
        // TODO: Implement after() method.
    }
}

and you can associate to a route in this way:, (*12)

$route->get('/', function(){
    echo 'home';
}, ['middleware' => CustomMiddleware::class]);

Group

If you want to set a middleware to a group of routes, you can declare a group of view:, (*13)

$route->group(['middleware' => CustomMiddleware::class], function () use ($route){
    $route->get('/users/:id{[0-9]*}', function ($id){
        echo 'user id ' . $id;
    });
    $route->get('/users/:name', function ($name){
        echo 'Welcome back ' . $name;
    });
})->dispatch();

License

The MIT License (MIT). Please see License File for more information., (*14)

The Versions

07/03 2017

dev-master

9999999-dev

Very fast request router

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Avatar Kito92

02/03 2017

1.0.0

1.0.0.0

Very fast request router

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Avatar Kito92