bake / bob
Very basic routing class
Requires
- php: >=5.4
This package is not auto-updated.
Last update: 2025-07-05 20:47:17 UTC
README
Very basic routing class (about 103 lines) ...
tl;dr
Bob::get($pattern, $callback);
is short for
Bob::add('get', $pattern, $callback);
$method
and $pattern
can either be strings or arrays of strings. $callback
s are one or more functions or a class.
Bob::go($file);
Usage
Routes
Add a route:
Bob::get('/', function() { echo 'Hello World'; });
A little bit more:
Bob::get('/user/bob', function() { echo 'Hey, bob!'; });
Add a bunch of patterns:
Bob::get(['/', '/home'], function() { echo 'Hello World'; });
Use a function:
Bob::get('/user/:is_numeric', function($id) { echo 'Hello, '.$user[$id]; });
Use an own function:
Bob::get('/user/:is_user', function($user) { echo 'Hey, '.$user.'!'; }); function is_user($user) { return in_array($user, ['Justus', 'Peter', 'Bob']); }
Negate:
Bob::get('/user/:is_user', function($user) { echo 'Hey, '.$user.'!'; }); Bob::get('/user/!is_user', function($user) { echo 'Can\'t find this user :('; });
You can also use regex (in the same way you'd use a function):
Bob::$patterns = [ 'num' => '[0-9]+', 'all' => '.*' ]; Bob::get('/:num', function($id) { echo $id; });
Callbacks
Use multiple callbacks:
Bob::get('/user/:is_numeric', [function($id) { echo 'Hello, '.$user[$id]; }, count_login($id)]);
Multiple request methods:
Bob::add(['post', 'put'], '/user', function() { // Add a user! Or something else! I don't care! });
A Class as Callback
Your can also use a class as callback. Just pass its name. Bob will try to execute $method
on $callback
, so something like this will work:
class SayHello { static function get($num) { for($i = 0; $i < $num; $i++) echo 'GET Hello!'; } static function post($num) { for($i = 0; $i < $num; $i++) echo 'POST Hello!'; } }
Bob::add([], '/:is_numeric', 'SayHello');
Notice that you have to use Bob::add()
with an empty array. In this case, you're not required to tell Bob the accepted HTTP methods, it'll just look inside the provided class.
Execute
With the use-an-own-function-example from above, the (second) final step could look like this:
Bob::go();
Or - if you'd like to work in a subdirectory - trim the request url:
Bob::go('/foo/bar.php');
http://localhost/foo/bar.php/user/1
=>
/user/1
If nothing was found
404:
Bob::summary(function($passed, $refused) { echo $passed.' routes passed, '.$refused.' were refused.'; });
This will only execute the callback, if no rule matched the request:
Bob::notfound(function($passed, $refused) { echo '404 :('; });
You're done. Have a nice day.