2017 © Pedro Peláez
 

library router

Simple PHP router class base on Macaw

image

hongjh/router

Simple PHP router class base on Macaw

  • Friday, May 20, 2016
  • by hongjh
  • Repository
  • 0 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Router

Router is a simple, open source PHP router base on Macaw. It's super small (~150 LOC), fast, and has some great annotated source code. This class allows you to just throw it into your project and start using it immediately., (*1)

Install

If you have Composer, just include Router as a project dependency in your composer.json. If you don't just install it by downloading the .ZIP file and extracting it to your project directory., (*2)

require: {
    "hongjh/router": "dev-master"
}

Examples

First, use the Router namespace:, (*3)

use \Hongjh\Router\Router;

Router is not an object, so you can just make direct operations to the class. Here's the Hello World:, (*4)

Router::get('/', function() {
  echo 'Hello world!';
});

Router::dispatch();

Router also supports lambda URIs, such as:, (*5)

Router::get('/(:any)', function($slug) {
  echo 'The slug is: ' . $slug;
});

Router::dispatch();

You can also make requests for HTTP methods in Router, so you could also do:, (*6)

Router::get('/', function() {
  echo 'I <3 GET commands!';
});

Router::post('/', function() {
  echo 'I <3 POST commands!';
});

Router::dispatch();

Lastly, if there is no route defined for a certain location, you can make Router run a custom callback, like:, (*7)

Router::error(function() {
  echo '404 :: Not Found';
});

If you don't specify an error callback, Router will just echo 404., (*8)

, (*9)


In order to let the server know the URI does not point to a real file, you may need to use one of the example configuration files., (*10)

The Versions

20/05 2016

dev-master

9999999-dev

Simple PHP router class base on Macaw

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Avatar hongjh