PHP code example of monomelodies / reroute

1. Go to this page and download the library: Download monomelodies/reroute library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

monomelodies / reroute example snippets




$router = new Router;
$router->when('/some/url/')->then(function () {
    // Return something.
});



$router = new Router;
$foo = $router->when('/foo/');
$bar = $foo->when('/bar/');
$baz = $bar->when('/baz/')->then('I match /foo/bar/baz/!');



$router->when('/some/url/')->then(function () {
    return 'Hello world!';
});;
$router->when('/some/url/')->then('Hello world!');

class Foo
{
    public static function getInstance()
    {
        return new Foo;
    }

    public function __invoke()
    {
        return 'Hello world!';
    }
}

$router->when('/some/url/')->then(new Foo);
$router->when('/some/url/')->then(['Foo', 'getInstance']);



use Zend\Diactoros\ServerRequestFactory;

if ($state = $router(ServerRequestFactory::fromGlobals())) {
    echo $state;
} else {
    // 404!
}



$router->when("/(?'name'\w+)/")->then(function ($name) {
    return "Hi there, $name!";
});



$router->when("/(?'param'.*?)/");
$router->when('/:param/');
$router->when('/{param}/');



use Psr\Http\Message\RequestInterface;

$router->when('/some/url/')->then(function (RequestInterface $request) {
    switch ($request->getMethod()) {
        case 'POST':
            // Perform some action
        case 'GET':
            return 'ok';
        default:
            return $request->getMethod()." method not allowed.";
    }
});



use Zend\Diactoros\Response\EmptyResponse;

$router->when('/some/url/')->then('my-awesome-state', function () {
    // Get not allowed!
    return new EmptyResponse(403);
})->post(function () {
    // ...do something, POST is allowed...
    // Since we disabled get, this should redirect somewhere valid afterwards.
});



$router->when('/some/url/')->then('my-state', function() {
    return 'This is a normal page';
})->post(function (callable $GET) {
    // Perform some action...
    return $GET;
});



$router->when('/foo/', function ($router) {
    $router->then('I match /foo/!');
    $router->when('/bar/')->then('I match /foo/bar/!');
});



$router->when('/foo/')->when('/bar/')->then('I match /foo/bar/!');
// ...or...
$foo = $router->when('/foo/');
$foo->when('/bar/')->then('I match /foo/bar/!');



$router->when('/foo/')
       ->then('I match /foo/!')
       ->when('/bar/')
       ->then('But I match /foo/bar/!');



$router->when('/restricted/')
    ->pipe(function ($payload) {
        if (!user_is_authenticated()) {
            // In the real world, probably raise an exception you can
            // catch elsewhere and show a login page or something...
            return null;
        }
        return $payload;
    })
    ->when('/super-secret-page/')
    ->then('For authenticated eyes only!');



$router->when("/(?'foo':\d+)/")
    ->pipe(function ($payload, $foo) {
        if ($foo != 42) {
            // return error response or something...
        }
        return $payload;
    });



$router->when('/:some/:params/')->then('myname', 'handler');
echo $router->generate('myname', ['some' => 'foo', 'params' => 'bar']);
// outputs: /foo/bar/



$router->when(null)->then('404', function() {
    return "The URL did an oopsie!";
});




if ($state = $router()) {
    echo $state;
} else {
    // Note that we must "invoke" the state.
    echo $router->get('404')();
}




$router->when('/the/url/')->then('myname', 'handler');
$state = $router->get('myname'); // Ok!
$state instanceof Reroute\State; // true