2017 © Pedro Peláez
 

package router

PHP Router based on memCrab Core API

image

memcrab/router

PHP Router based on memCrab Core API

  • Tuesday, October 31, 2017
  • by noonehos
  • Repository
  • 2 Watchers
  • 1 Stars
  • 1,686 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 19 Versions
  • 21 % Grown

The README.md

PHP Router as Composer Library

Status

Build Status Dependency Status Total Downloads Latest Stable Version Latest Unstable Version License composer.lock available, (*1)

It's php router based on yaml configuration file and support regular expressions in each route condition. Thats help build more accurate routes with only numbers in part of url or with required part of word etc., (*2)

Features

  • Support RegExp in any kind of route
  • Support multiple routings for single url throw different request methods (POST, GET, PUT, DELETE, ...)
  • Support full url or just request uri
  • All configurations in simple YAML file
  • Each route can return already named params (as many params as you want, or as you have in Regular Expression)
  • High performance yaml parse throw using updated pecl yaml-ext 2.0.0 for php 7.0
  • Strict standart coding with full Typing of params and returns (by php 7.1)
  • PSR-4 autoloading compliant structure
  • Unit-Testing with PHPUnit
  • Easy to use to any framework

Install

composer require memcrab/router, (*3)

Dependencies

php extension YAML: - for Ubuntu/Debian, (*4)

- apt-get update
- apt-get install php-pear
- apt-get install php-dev
- apt-get install php-xml php7.0-xml
- apt-get install libyaml-dev
- pecl channel-update pecl.php.net
- pecl install yaml-2.0.0
  • for OS X
- brew install php71 --with-pear
- brew install autoconf
- touch $(brew --prefix php71)/lib/php/.lock && chmod 0644 $(brew --prefix php71)/lib/php/.lock
- pecl install yaml-2.0.0

Usage

  • init Router: memCrab\Router()
  • load routes: ->loadRoutesFromYaml(string $filePath)
    • $filePath - Path to yaml files with routes
  • run matching: ->matchRoute(string $url, string $method)
    • $url - URL (http://example.com/posts) or just request URI of page (/post)
    • $method - http request method
  • use your router data with:
    • getService() - return component that we call
    • getAction() - return action that will be run from component
    • getParams() - return route regExp params

Yaml Config Example

/:
  GET: [Index, getMain]
/post/:
  GET:    [Post, get]
  POST:   [Post, add]
  PATCH:  [Post, save]
  DELETE: [Post, delete]
/post/publish/:
  POST: [Post, setPublishing]
/catalog/([a-zA-Z0-9]+)-([a-zA-Z0-9]+)/: 
  GET: [Catalog, filter, key1, value1]

Run Example

<?php
declare (strict_types = 1);
require_once __DIR__ . "/vendor/autoload.php";

use memCrab\Exceptions\FileException;
use memCrab\Exceptions\RoutingException;
use memCrab\File\Yaml;
use memCrab\Router\Router;

try {
  # Read routes from yaml
  $Yaml = new Yaml();

  $routes = $Yaml->load("../src/routs.example.yaml", null)->getContent();

  # For enable cache You can use FileCache object as second parametr of
  # $Yaml->load() function. Use memCrab\Cache library for it.
  # Redis Cache: $FileCache = new RedisCache([Redis obj]);
  # PHP file Cache: $FileCache = new PHPCache([PathToTMLFolder]);

  # Initialize Router
  $Router = new Router();
  $Router->loadRoutes($routes);

  # Routing
  $Router->matchRoute("http://example.com/post/", "POST");
  # Run your Controller|Service|Component
  $ServiceName = $Router->getService();
  $ActionName = $Router->getAction();
  $Service = new $ServiceName();
  $Response = $Service->$Action($Router->getParams());
} catch (RoutingException $error) {
  $Respose = new \Response();
  $Respose->setErrorResponse($error);
} catch (FileException $error) {
  $Respose = new \Response();
  $Respose->setErrorResponse($error);
}

$Respose->sendHeaders();
$Respose->sendContent();

TODOS

  • [ ] Add support for suffixes - right part of uri that not involved in routing like .html, .php, last "/", etc
  • [ ] Add support for prefixes - left part of uri that not involved in routing like lang part (uk/us/fr/ru) or geo part (europe/asia), etc

MIT Licensed, (*5)

The Versions

31/10 2017

dev-master

9999999-dev http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

31/10 2017

1.3.10

1.3.10.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

31/10 2017

1.3.9

1.3.9.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

14/08 2017

1.3.8

1.3.8.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

09/08 2017

1.3.7

1.3.7.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

01/04 2017

1.3.6

1.3.6.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

25/02 2017

1.3.5

1.3.5.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

16/02 2017

1.3.4

1.3.4.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

16/02 2017

1.3.3

1.3.3.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

16/02 2017

1.3.2

1.3.2.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml >=2.0.0

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

01/02 2017

1.3.1

1.3.1.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml >=2.0.0

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

01/02 2017

1.3.0

1.3.0.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml >=2.0.0

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

01/02 2017

1.2.4

1.2.4.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml >=2.0.0

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

01/02 2017

1.2.3

1.2.3.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml >=2.0.0

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

01/02 2017

1.2.2

1.2.2.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml >=2.0.0

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

01/02 2017

1.2.1

1.2.1.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml >=2.0.0

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

31/01 2017

1.2.0

1.2.0.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml >=2.0.0

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

31/01 2017

1.1.2

1.1.2.0 http://memcrab.com

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml >=2.0.0

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions

30/01 2017

1.0.0

1.0.0.0

PHP Router based on memCrab Core API

  Sources   Download

MIT

The Requires

  • php >=7.0
  • ext-yaml >=2.0.0

 

The Development Requires

by Oleksandr Diudiun

php yaml router regexp expressions