Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/usmanhalalit/140-chars-router
URL routing script for PHP which is written in just 140 characters of code, so it fits in a tweet.
https://github.com/usmanhalalit/140-chars-router
Last synced: about 2 months ago
JSON representation
URL routing script for PHP which is written in just 140 characters of code, so it fits in a tweet.
- Host: GitHub
- URL: https://github.com/usmanhalalit/140-chars-router
- Owner: usmanhalalit
- Created: 2013-07-06T10:11:45.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2017-07-12T11:04:05.000Z (over 7 years ago)
- Last Synced: 2024-04-15T00:14:08.131Z (8 months ago)
- Language: PHP
- Homepage:
- Size: 1.95 KB
- Stars: 28
- Watchers: 5
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PHP Router in 140 Characters
This is a dimple(damn simple) URL routing script for PHP which is written in just 140 characters of code, so it fits within a tweet.
Its NOT a good router at all, **don't use it on a real application**, its not exception handled either. I did it just for fun and to demonstrate the simplicity
So here's the router code, and that's all (seriously).
```PHP
class R{
function a($r,callable $c){$this->r[$r]=$c;}
function e(){$s=$_SERVER;$i='PATH_INFO';$p=isset($s[$i])?$s[$i]:'/';$this->r[$p]();}
}
```## Usage
Create a file index.php in your server root and try this code
```PHP
r[$r]=$c;}
function e(){$s=$_SERVER;$i='PATH_INFO';$p=isset($s[$i])?$s[$i]:'/';$this->r[$p]();}
}
//// Create
$router = new R();// Add a route with a callback function
$router->a('/a', 'callbackFunction');// Add a route with a closure
$router->a('/b', function(){
echo 'Hello B';
});// Add homepage route
$router->a('/', function(){
echo 'Hello World';
});// Add route with class method
$router->a('/c', [new Foo, 'bar']);
// Add multiple slashed route with class method
$router->a('/c/d', [new Foo, 'bar']);// Execute the route
$router->e();// Callback handlers
function callbackFunction(){
echo 'Hello A';
}class Foo{
function bar(){
echo 'Hello Bar';
}
}
```Now visit your server root `http://localhost/index.php`, `http://localhost/index.php/a`, `http://localhost/index.php/b`, `http://localhost/index.php/c` and `http://localhost/index.php/c/d`.
Or you may run built-in PHP server from the command line (in the same dir)
```
php -S localhost:8081
```
and visit `http://localhost:8081/index.php`.Runs on 5.4+. 140 chars idea is inspired by Fabien Potencier's [Twittee](http://twittee.org/).