1. Go to this page and download the library: Download ertuo-php/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/ */
ertuo-php / router example snippets
use Ertuo\Route;
$routes = Route::add('_locale')
->rule('enum', ['en', 'de'])->default('en')
->group(function()
{
yield '' => Route::add('_controller')
->group(function()
{
yield 'blog' => Route::add('', ['_route' => 'blog'])
->default('page')
->group(function()
{
yield 'page' => Route::add('_page', ['_route' => 'blog_page'])
->rule('format', ['int'])->default(1);
});
});
});
// Turn the request URI into array
$src = Ertuo\Kit::quickExplode('/blog/page/4/');
// Parse the $src array into a routing result
$result = (new Ertuo\Dispatcher($routes))->dispatch($src);
print_r($result);
/* Ertuo\Result Object
(
[path] => Array
(
[0] => blog
[1] => page
[2] => 4
)
[attributes] => Array
(
[_locale] => en
[_controller] => blog
[_route] => blog_page
[_page] => 4
)
[junk] => Array
(
)
) */
// create a composite rule from "id" and "any" rules
$agg->lazy('blog_post',
fn() => new Ertuo\Rule\CompositeRule(
$agg->fetchRule('id'),
$agg->fetchRule('any')
)
);
use Ertuo\Result;
use Ertuo\Rule\RuleInterface;
class HanShotFirstRule implements RuleInterface
{
function accept(string $step, array $options, Result $result) : bool
{
return !empty($step) && !empty($result->attributes['han.shot']);
}
}
use Ertuo\Route;
use Ertuo\UnfoldedRoute;
$unfoldedRoutesFilename = '/somewhere/where/routes/live/unfolded.php';
if (!is_file($unfoldedRoutesFilename))
{
$routes = Route::add('_locale')->rule( ... );
file_put_contents(
$unfoldedRoutesFilename,
' return '
. var_export($routes->toArray(), true)
. ';'
);
}
$unfolded =
$src = []; // source array for "/" request URI
$result = (new Ertuo\Dispatcher($routes))->dispatch($src);
print_r($result);
$src = []; // homepage in "en" (default locale)
$src = ['de']; // homepage in "de"
$src = ['contact']; // contact in "en"
$src = ['search', 'test', 'unit']; // search in "en" for "test" and "unit"
$src = ['blog']; // blog in "en", browsing page=1
$src = ['en', 'blog', '2']; // blog in "en", browsing page=2
$src = ['en', 'blog', 'page', '4']; // blog in "en", browsing page=4
$src = ['de', 'blog', 'post']; // blog in "de", missing post slug, slug="404"
$src = ['blog', 'post', 'the-goat']; // blog in "en", post slug="the-goat"
$src = ['en', 'about-us']; // content in "en"
$src = ['index']; // bad locale, no such content page declared
$src = ['en', 'blog', 'page', 'x5']; // bad page format
$src = ['blog', 'post', 'i_know']; // bad slug format