2017 © Pedro Peláez
 

library php-router

Simple PHP Router, supports REST and reverse routing.

image

dannyvankooten/php-router

Simple PHP Router, supports REST and reverse routing.

  • Sunday, April 29, 2018
  • by dannyvankooten
  • Repository
  • 39 Watchers
  • 502 Stars
  • 21,960 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 128 Forks
  • 13 Open issues
  • 8 Versions
  • 1 % Grown

The README.md

PHP Router class

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

A simple Rails inspired PHP router class., (*2)

  • Usage of different HTTP Methods
  • REST / Resourceful routing
  • Reversed routing using named routes
  • Dynamic URL's: use URL segments as parameters.

Authors

Easy to install with composer

$ composer require dannyvankooten/php-router

Usage

Friendly URL

Create a simple .htaccess file on your root directory if you're using Apache with mod_rewrite enabled., (*3)

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php [NC,L]

If you're using nginx, setup your server section as following:, (*4)

server {
    listen 80;
    server_name mydevsite.dev;
    root /var/www/mydevsite/public;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        fastcgi_intercept_errors on;
    }
}

This is a simple example of routers in action, (*5)

<?php
require __DIR__.'/vendor/autoload.php';

use PHPRouter\RouteCollection;
use PHPRouter\Router;
use PHPRouter\Route;

$collection = new RouteCollection();
$collection->attachRoute(new Route('/users/', array(
    '_controller' => 'someController::usersCreate',
    'methods' => 'GET'
)));

$collection->attachRoute(new Route('/', array(
    '_controller' => 'someController::indexAction',
    'methods' => 'GET'
)));

$router = new Router($collection);
$router->setBasePath('/PHP-Router');
$route = $router->matchCurrentRequest();

var_dump($route);

Load routers from a yaml file

We can define in a yaml file all the routes of our application. This facilitates our life when we need to migrate, modify, or later add new routes., (*6)

The route definition should follow the example below:, (*7)

base_path: /blog

routes:
  index: [/index, someClass.indexAction, GET]
  contact: [/contact, someClass.contactAction, GET]
  about: [/about, someClass.aboutAction, GET]

In our Front Controller would have something like:, (*8)

<?php
require __DIR__.'/vendor/autoload.php';

use PHPRouter\RouteCollection;
use PHPRouter\Config;
use PHPRouter\Router;
use PHPRouter\Route;

$config = Config::loadFromFile(__DIR__.'/router.yaml');
$router = Router::parseConfig($config);
$router->matchCurrentRequest();

More information

If you like PHP Router you might also like AltoRouter., (*9)

License

MIT Licensed, http://www.opensource.org/licenses/MIT, (*10)

The Versions

29/04 2018

dev-master

9999999-dev https://github.com/dannyvankooten/PHP-Router

Simple PHP Router, supports REST and reverse routing.

  Sources   Download

MIT

The Requires

 

The Development Requires

rest routing router

25/04 2017

dev-di-implementation

dev-di-implementation https://github.com/dannyvankooten/PHP-Router

Simple PHP Router, supports REST and reverse routing.

  Sources   Download

MIT

The Requires

 

The Development Requires

rest routing router

09/03 2017

dev-hotfix-101

dev-hotfix-101 https://github.com/dannyvankooten/PHP-Router

Simple PHP Router, supports REST and reverse routing.

  Sources   Download

MIT

The Requires

 

The Development Requires

rest routing router

09/03 2017

1.2.0-alpha

1.2.0.0-alpha https://github.com/dannyvankooten/PHP-Router

Simple PHP Router, supports REST and reverse routing.

  Sources   Download

MIT

The Requires

 

The Development Requires

rest routing router

10/01 2017

dev-revert-93-hotfix/phpcs

dev-revert-93-hotfix/phpcs https://github.com/dannyvankooten/PHP-Router

Simple PHP Router, supports REST and reverse routing.

  Sources   Download

MIT

The Requires

 

The Development Requires

rest routing router

31/01 2015

1.1.0-alpha

1.1.0.0-alpha https://github.com/dannyvankooten/PHP-Router

Simple PHP Router, supports REST and reverse routing.

  Sources   Download

MIT

The Requires

 

The Development Requires

php rest routing router

26/08 2014

dev-1.0.0-a

dev-1.0.0-a https://github.com/dannyvankooten/PHP-Router

Simple PHP Router, supports REST and reverse routing.

  Sources   Download

MIT

The Requires

 

The Development Requires

php rest routing router

12/02 2014

1.0.0-alpha

1.0.0.0-alpha https://github.com/dannyvankooten/PHP-Router

Simple PHP Router, supports REST and reverse routing.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

php rest routing router