2017 © Pedro Peláez
 

library sen-router

Fast router for php with regex, middleware route separator support

image

touskar/sen-router

Fast router for php with regex, middleware route separator support

  • Saturday, February 3, 2018
  • by Touskar
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

    
 _________                       __________               __                
 /   _____/ ____   ____           \______   \ ____  __ ___/  |_  ___________ 
 \_____  \_/ __ \ /    \   ______  |       _//  _ \|  |  \   __\/ __ \_  __ \
 /        \  ___/|   |  \ /_____/  |    |   (  <_> )  |  /|  | \  ___/|  | \/
/_______  /\___  >___|  /          |____|_  /\____/|____/ |__|  \___  >__|   
        \/     \/     \/                  \/                        \/    
------------------------------------------------------------------------------ 

Info

Click to see SenRouter in Packagist, (*1)

Install

composer require touskar/sen-router, (*2)

Usage

.htacess for frontal controller

``` .htaccess SetEnv FRONTAL_CONTROLER index.php SetEnv FRONTAL_CONTROLER_SUB_DIR /web/root/ # or / for non subdired project, (*3)

Options -MultiViews RewriteEngine On, (*4)

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %{ENV:FRONTAL_CONTROLER} [L]

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

, (*5)



#### index.php ```php require_once('../vendor/autoload.php'); use SenRouter\Http\Dispatcher\Router; use SenRouter\Http\Dispatcher\R; $router = new Router(); /** * call from $router Object **/ $router ->get('hello/{name}', function ($name) { return "Hello $name"; }); /** * call from static method **/ R::('hello.{name}', function ($name) { return "Hello $name"; }) ->separator("."); /** * Call from static method * mixte route params and query params * https://example.com/user/moussa_ndour@hotmail.fr/bicis?order_by=name */ R::get('users/{email}/{entreprise}', function ($email, $entreprise) { $orderBy = Input::one('order_by'); return "Hello $email $entreprise with query $orderBy"; }); /** * Call from static method **/ R::get('calcul.{num1}.{num2}', function ($num1, $num2) { return Response::withJson([ 'result' => $num1 + $num2 ]); }) ->regex([ 'num1' => '\d+', 'num2' => '\d+' ]) ->middleware([ 'HomeMiddleware@isOdd', function ($num1, $num2) { if ($num1 == $num2) { return false; } } ]) ->separator("."); $router->run(); //or R::run();

HomeController

use SenRouter\Http\Response;
class HomeController{

    public function sum($num1, $num2){
        return Response::withJson([
            'result' => $num1 + $num2
        ]);
    }
}

HomeMiddleware

use SenRouter\Http\Response;
class HomeMiddleware{

     /**
      * Return strict value False or no empty string to block
      * returned value will be send as request response
      */
    public function isOdd($num1, $num2){
        if($num1 % 2 !== 0 || $num2 % 2 !== 0 )
        {
            return false;// return 'some_string';
        }
    }
}

The Versions

03/02 2018

dev-master

9999999-dev

Fast router for php with regex, middleware route separator support

  Sources   Download

by moussandour