PHP code example of ride / lib-router

1. Go to this page and download the library: Download ride/lib-router 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/ */

    

ride / lib-router example snippets




use ride\library\router\GenericRouter;
use ride\library\router\RouteContainer;

// create a route container
$routeContainer = new RouteContainer();

// create a route with a path and a php callback
$route = $routeContainer->createRoute('/path/to/%action%', 'callback', 'id');
// single method allowed
$route->setAllowedMethods('GET'); 
// multiple methods allowed, case does not matter
$route->setAllowedMethods(array('GET', 'post'));

// add the route to the route container
$routeContainer->setRoute($route);

// create an alias
$alias = $routeContainer->createAlias('/path/to/content', '/ptc');

// add the alias to the route container
$routeContainer->setAlias($alias);

// create the router
$router = new GenericRouter($routeContainer);
// set a default action for the / request
$router->setDefaultCallback('callback');

// match a route
$result = $router->route('GET', '/foo/bar');

// no match
$result->isEmpty(); // true

// let's try again
$result = $router->route('PUT', '/path/to/content');

// a match but nothing to invoke
$result->isEmpty(); // false
$result->getAlias(); // null
$result->getRoute(); // null
$result->getAllowedMethods(); // array('GET' => true, 'POST' => true)

// now with the right method
$result = $router->route('GET', '/path/to/content');

// a match with arguments set to the route
$result->isEmpty(); // false
$result->getAlias(); // null
$result->getRoute(); // Route instance
$result->getRoute()->getArguments(); // array('action' => 'content');

// what about the alias?
$result = $router->route('GET', '/ptc');

// the same match will be generated
$result->isEmpty(); // false
$result->getAlias(); // null
$result->getRoute(); // Route instance
$result->getRoute()->getArguments(); // array('action' => 'content');

// let's force the alias
$alias->setIsForced(true);

// what about the alias now?
$result = $router->route('GET', '/ptc');

// still the same
$result->isEmpty(); // false
$result->getAlias(); // null
$result->getRoute(); // Route instance
$result->getRoute()->getArguments(); // array('action' => 'content');

// but when we take our original request ...
$result = $router->route('GET', '/path/to/content');

// ... we see we need to redirect
$result->isEmpty(); // false
$result->getAlias(); // Alias instance

// let's test multi domain support
$route = new Route('/path', 'callback', 'id2');
$route->setBaseUrl('http://some-server.com');    
$routeContainer->setRoute($route);

$result = $router->route('GET', '/path', 'http://other-server.com');
$result->isEmpty(); // true

$result = $router->route('GET', '/path', 'http://some-server.com');
$result->isEmpty(); // false

// create some urls

// http://some-server.com/path
$url = $routeContainer->getUrl('http://my-server.com', 'id2');
 
// http://my-server.com/ptc
$url = $routeContainer->getUrl('http://my-server.com', 'id', array('action' => 'content'));
 
// http://my-server.com/path/to/my-action
$routeContainer->getUrl('http://my-server.com', 'id', array('action' => 'my-action'));
 
// http://my-server.com/path/to/my-action?limit=20&page=1
$url = $routeContainer->getUrl('http://my-server.com', 'id', array('action' => 'my-action'), array('page' => 1, 'limit' => 20));
 
// http://my-server.com/path/to/your-action?limit=20&amp;page=2
$url = $routeContainer->getUrl('http://my-server.com', 'id', array('action' => 'my-action'), array('page' => 1, 'limit' => 20), '&amp;');
$url->setArgument('action', 'your-action');
$url->setQueryParameter('page', 2);
 
// translates an URL to it's alias if available and needed
$url = $routeContainer->getUrlAlias($url);