PHP code example of gobline / router

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

    

gobline / router example snippets


$router = (new Gobline\Router\LiteralRoute('/user/profile')) // profile is the route name and /user/profile is the route to match
    ->setName('profile')
    ->values([
        'controller' => 'user',
        'action' => 'profile',
    ]);

$router = (new Gobline\Router\LiteralRoute('/user/profile'))
    ->setName('profile')
    ->values([
        'controller' => 'user',
        'action' => 'profile',
    ])
    ->i18n([
        'fr' => '/membre/profil',
        'nl' => '/gebruiker/profiel',
    ]);

$router = (new Gobline\Router\PlaceHolderRoute('/user/:id(/)(/articles/:action(/))'))
    ->setName('profile')
    ->values([
        'controller' => 'articles',
        'action' => 'list',
    ])
    ->constraints([
        'id' => '[0-9]+',
        'action' => '[a-zA-Z]+',
    ]);

$router = (new Gobline\Router\PlaceHolderRoute('/user/:id(/)(/articles/:action(/))'))
    ->setName('profile')
    ->values([
        'controller' => 'articles',
        'action' => 'list',
    ])
    ->constraints([
        'id' => '[0-9]+',
        'action' => '[a-zA-Z]+',
    ])
    ->i18n([
        'fr' => '/membre/:id(/)(/articles/:action(/))',
        'nl' => '/gebruiker/:id(/)(/artikelen/:action(/))',
        'placeholders' => [
            'action' => [
                'fr' => [
                    'list' => 'liste',
                ],
                'nl' => [
                    'list' => 'lijst',
                ],
            ],
        ],
    ]);

$routeCollection = new Gobline\Router\RouteCollection();
$routeCollection
    ->get(new Gobline\Router\LiteralRoute(/*...*/))
    ->post(new Gobline\Router\PlaceHolderRouter(/*...*/));

$requestMatcher = new Gobline\Router\RequestMatcher($routeCollection);
$routeData = $requestMatcher->match($request); // psr-7 server request

$uriBuilder = new Gobline\Router\UriBuilder($routerCollection);

$url = $uriBuilder->makeUrl(new Gobline\Router\RouteData('profile', ['id' => 42]));