2017 © Pedro Peláez
 

library router

PHP Router for PHP5.4+ Built-in Server

image

chansig/router

PHP Router for PHP5.4+ Built-in Server

  • Saturday, April 9, 2016
  • by Chansig
  • Repository
  • 1 Watchers
  • 2 Stars
  • 110 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 17 Versions
  • 1 % Grown

The README.md

Chansig/Router

PHP Router for PHP5.4+ Built-in Server, (*1)

See http://php.net/manual/en/features.commandline.webserver.php, (*2)

Works on:, (*3)

  • Wordpress
  • Symfony 2,
  • Laravel,
  • phpMyAdmin,
  • bolt,
  • etc.

Features

  • Serve multiple domains with vhosts configuration
  • Allow different directory index (app_dev.php for example)
  • Allowed Hosts only
  • Header Cache control
  • Header Access-Control-Allow-Origin
  • Handles 404 error page
  • Access logs

Requirement

PHP 5.4+, (*4)

Warning

The built-in PHP web server is NOT meant to be a replacement for any production web server. It should used for development purposes only!, (*5)

Installation

The recommended way to install Chansig/Router is through Composer., (*6)

# Install Composer
curl -sS https://getcomposer.org/installer | php

Next, run the Composer command to install the latest stable version of Chansig/Router:, (*7)

composer.phar require chansig/router dev-master

You can then later update Chansig/Router using composer:, (*8)

composer.phar update

Run PHP Built-in Server

php -S <addr>:<port> -t <docroot> vendor/chansig/router/src/router.php

e.g. php -S localhost:80 vendor/chansig/router/src/router.php, (*9)

e.g. on Symfony:, (*10)

php -S 127.0.0.1:8080 -t web vendor/chansig/src/router/router_symfony_dev.php

e.g. on Wordpress:, (*11)

php -S localhost:81 -t wordpress vendor/chansig/src/router/router.php

Using Symfony console

app/console server:run 127.0.0.1:80 --router=vendor/chansig/src/router/router_symfony_dev.php --docroot=web

php.ini

override ini values, (*12)

 php -S localhost:81 -t wordpress -c my.ini router.php

Configuration

  • copy vendor/chansig/src/router/router.php in your main directory
  • require Composer's autoloader in router.php:, (*13)

    #router.php
    require 'vendor/autoload.php';

Override configuration

  • copy vendor/chansig/src/router/router.json.example in your main directory
  • copy router.json.example to router.json

Set configuration values in router.json:, (*14)

  • "hosts-name"
    @var string[] []
    List of allowed hosts if not empty., (*15)

  • "port"
    @var null|int null
    Which tcp/ip port does the server run on. Default null for all ports, (*16)

  • "docroot"
    @var null|string null
    Override Server DOCUMENT_ROOT if not null, (*17)

  • "directory-index"
    @var null|array ["index.php", "index.html"]
    Directory index filenames., (*18)

  • "rewrite-index"
    @var null|array null
    Rewrite filenames in docroot., (*19)

    e.g., (*20)

        "rewrite-index": ["app_dev.php"]  
  • "allow-origin"
    @var null|array null
    Send Header Access-Control-Allow-Origin for defined hosts.
    Useful for fonts files required on local CDN or Ajax requests., (*21)

    e.g., (*22)

        "allow-origin": ["*"]
  • "handle-404"
    @var bool false
    Router handles 404 error page., (*23)

  • "cache-control" @var null|int null
    Send http cache headers if > 0. Ex: Cache-Control: public, max-age=300, (*24)

    e.g., (*25)

        "cache-control": 86400
  • "render-ssi" @var bool false Render SSI include virtual & file., (*26)

    e.g., (*27)

        "render-ssi": true
  • "ext-ssi" @var null|array ["shtml", "html", "htm"] File extensions that contains an SSI directive., (*28)

    e.g., (*29)

        "ext-ssi": ["shtml"]
  • "log"
    @var bool true
    Send access logs to output if true, (*30)

  • "logs-dir"
    @var null|string null
    Write access logs to logs-dir if not null, (*31)

    e.g., (*32)

        "logs-dir": "/Users/Toto/Sites/mysite.fr/app/logs"
  • "auto-index-file"
    @var null|string null
    PHP index Directory for directory listing. @see Chansig/DirectoryIndex., (*33)

    To add auto-index-file, composer install chansig/directoryindex.
    Set "auto-index-file" to absolute path of file vendor/chansig/directoryindex/src/directory.php., (*34)

    e.g., (*35)

        "auto-index-file": "/var/www/myphotos/vendor/chansig/directoryindex/src/directory.php"  
  • "vhosts"
    @var object null
    List of virtual hosts., (*36)

    You must define server(s) name and document root for each vhost.
    Configuration is the same as the global configuration.
    Vhost configuration is merged into global configuration., (*37)

    e.g., (*38)

    #router.json
    {
        "hosts-name":  [],
        "port": null,
        "docroot": null,
        "directory-index": ["index.php"],
        "rewrite-index": null,
        "allow-origin":  null,
        "allow-origin":  null,
        "handle-404":  false,
        "cache-control":  0,
        "log":  true,
        "logs-dir":  null,
        "auto-index-file":  null,
        "vhosts":{
            "serverkey1": {
                "hosts-name": ["dev.mysite.ltd", "dev.www.mysite.ltd"],
                "docroot": "/var/www/www.mysite.tld",
                "rewrite-index": ["app_dev.php"],
                "logs-dir": "/var/log/php/mysite.ltd",
            },
            "serverkey1": {
                "hosts-name": ["cdn.dev.mysite.ltd""],
                "port": 8080,
                "docroot": "/var/www/www.mysite.tld",
                "directory-index": ["index.html", "mydirectoryindex.php"],
                "allow-origin": null,
                "cache-control": 43200,
                "handle-404": true,
                "log": false
            }
        }
    }

    In vhosts configuration, hosts-name can be a regex. Captured patterns are available in $1 to $n string., (*39)

    e.g., (*40)

    "hosts-name": ["dev.www.([a-z]+).([a-z]+)"],

    They will be replaced in docroot and logs-dir values., (*41)

    e.g., (*42)

    "docroot": "/var/www/www.$1.$2/web",
    "logs-dir": "/var/log/php/www.$1.$2", 
  • "include"
    @var null|array []
    Include external config files.
    Don't override main config., (*43)

    e.g., (*44)

        "include": [
            "/Users/Toto/Sites/mysite.fr/router.json"
        ]

You can skip default configuration:
For exemple, sf2 site on windows:, (*45)

#router.json
        {
            "vhosts":{
                "mysite": {
                    "hosts-name": ["dev.mysite.ltd", "dev.www.mysite.ltd"],
                    "docroot": "C:\\var\\www\\www.mysite.tld\\web",
                    "rewrite-index": ["app_dev.php"],
                    "logs-dir": "C:\\var\\www\\www.mysite.tld\\app\\logs",",
                },
                "mysite2": {
                    "hosts-name": ["dev.www.mysite2.ltd"],
                    "docroot": "C:\\var\\www\\www.mysite2.tld\\web",
                    "rewrite-rewrite": ["app_dev.php"],
                    "logs-dir": "C:\\var\\www\\www.mysite2.tld\\app\\logs",",
                },
                "directory": {
                    "hosts-name": ["dev.www.mysite3.ltd"],
                    "docroot": "C:\\var\\www\\www.mysite23.tld",
                    "auto-index-file": "C:\\var\\www\\vendor\\chansig\\directoryindex\\directory.php"
                },
            }
        }
  • load config into router, (*46)

    $config = json_decode(file_get_contents(__DIR__ . '/router.json'), true);
    $router = new PhpRouter($config);
    
    or
    
    $router = new PhpRouter(__DIR__ . '/router.json');
  • run server, (*47)

    php -S localhost:80 router.php
    
    To find process:
    ps -ef | grep router.php

Warning

On OSX, run sudo php -S 127.0.0.1:{port} router.php for port < 1024, (*48)

PHP Build In Server is single threaded. So you can not curl (or file_ge_content) to itself on same port. Run an other Server on a new Port to do it., (*49)

e.g. php -S 127.0.0.1:80 for front and php -S 127.0.0.1:8000 for api, (*50)

License

MIT License, (*51)

Author

Chansig., (*52)

The Versions

09/04 2016

dev-master

9999999-dev https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

09/04 2016

v0.7.4

0.7.4.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

09/04 2016

v0.7.3

0.7.3.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

13/09 2015

v0.7.2

0.7.2.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

08/09 2015

v0.7.1

0.7.1.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

06/09 2015

v0.7.0

0.7.0.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

02/09 2015

dev-develop

dev-develop https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

02/09 2015

v0.6.2

0.6.2.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

30/08 2015

v0.6.1

0.6.1.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

30/08 2015

v0.6.0

0.6.0.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

04/08 2015

v0.5.1

0.5.1.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

02/08 2015

v0.5.0

0.5.0.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

01/08 2015

v0.4.0

0.4.0.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress laravel server symfony router phpmyadmin

29/07 2015

v0.3.1

0.3.1.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress server symfony router

26/07 2015

v0.3.0

0.3.0.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress server symfony router

26/07 2015

0.2

0.2.0.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress server symfony router

19/07 2015

0.1

0.1.0.0 https://github.com/Chansig/Router

PHP Router for PHP5.4+ Built-in Server

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar Chansig

wordpress server symfony router