mombol/router

a php router base on Macaw

1.0.4 2016-04-22 11:11 UTC

This package is not auto-updated.

Last update: 2024-09-12 00:21:09 UTC


README

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.

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.

require: {
    "mombol/router": "^1.0"
}

Examples

First, use the Router namespace:

use \Mombol\Router\Router;

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

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

Router also supports lambda URIs, such as:

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

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

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

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

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

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

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

After call Router function do this:

Router::dispatch(function($content){
  if (!empty($content) && is_string($content)) {
    echo $content;
  }
});

Orther

See https://github.com/noahbuscher/Macaw