2017-25 © Pedro Peláez
 

library bob

Very basic routing class

image

bake/bob

Very basic routing class

  • Wednesday, September 7, 2016
  • by BakeRolls
  • Repository
  • 1 Watchers
  • 2 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Bob

Very basic routing class (about 103 lines) ..., (*1)

tl;dr

Bob::get($pattern, $callback);

is short for, (*2)

Bob::add('get', $pattern, $callback);

$method and $pattern can either be strings or arrays of strings. $callbacks are one or more functions or a class., (*3)

Bob::go($file);

Usage

Routes

Add a route:, (*4)

Bob::get('/', function() {
    echo 'Hello World';
});

A little bit more:, (*5)

Bob::get('/user/bob', function() {
    echo 'Hey, bob!';
});

Add a bunch of patterns:, (*6)

Bob::get(['/', '/home'], function() {
    echo 'Hello World';
});

Use a function:, (*7)

Bob::get('/user/:is_numeric', function($id) {
    echo 'Hello, '.$user[$id];
});

Use an own function:, (*8)

Bob::get('/user/:is_user', function($user) {
    echo 'Hey, '.$user.'!';
});

function is_user($user) {
    return in_array($user, ['Justus', 'Peter', 'Bob']);
}

Negate:, (*9)

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):, (*10)

Bob::$patterns = [
    'num' => '[0-9]+',
    'all' => '.*'
];

Bob::get('/:num', function($id) {
    echo $id;
});

Callbacks

Use multiple callbacks:, (*11)

Bob::get('/user/:is_numeric', [function($id) {
    echo 'Hello, '.$user[$id];
}, count_login($id)]);

Multiple request methods:, (*12)

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:, (*13)

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., (*14)

Execute

With the use-an-own-function-example from above, the (second) final step could look like this:, (*15)

Bob::go();

Or - if you'd like to work in a subdirectory - trim the request url:, (*16)

Bob::go('/foo/bar.php');

http://localhost/foo/bar.php/user/1 => /user/1, (*17)

If nothing was found

404:, (*18)

Bob::summary(function($passed, $refused) {
    echo $passed.' routes passed, '.$refused.' were refused.';
});

This will only execute the callback, if no rule matched the request:, (*19)

Bob::notfound(function($passed, $refused) {
    echo '404 :(';
});

You're done. Have a nice day., (*20)

The Versions

07/09 2016

dev-master

9999999-dev

Very basic routing class

  Sources   Download

The Requires

  • php >=5.4