8000 GitHub - mombol/php-router: a php micro router base on Macaw
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

mombol/php-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

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.

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

About

a php micro router base on Macaw

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

0