The purpose of routing is to map a URL to an array of data or to a callback, allowing to route the request to the correct resource. In an MVC context, this is typically a controller and a controller action.
There are two routers bundled by default:
Gobline\Router\LiteralRoute
(with its i18n friendGobline\Router\I18n\LiteralRoute
)Gobline\Router\PlaceHolderRoute
(with its i18n friendGobline\Router\I18n\PlaceHolderRoute
)
The Router component allows to have your URLs translated in multiple languages, allowing to have more SEO- and user-friendly URLs.
The Literal route is for doing exact matching of the URI path.
$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]));
You can install the Router component using the dependency management tool Composer. Run the require command to resolve and download the dependencies:
composer require gobline/router