<?php
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Loader\ClosureLoader;
// a collection of routes and their controllers
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo/{baz}', [
'_controller' => function ($baz) {
dump($baz);
},
]));
$collection->add('bar', new Route('/bar/{baz}/{qux}', [
'_controller' => function ($baz, $qux) {
dump($baz + $qux);
},
]));
// create Router
$closure = function () use ($collection) {
return $collection;
};
$loader = new ClosureLoader();
$router = new Router($loader, $closure);
// matching path of url
$match1 = $router->match('/foo/test');
$match2 = $router->match('/bar/3/5');
// function for executing controller
$executeController = function (array $match) {
$controller = $match['_controller'];
unset($match['_controller'], $match['_route']);
$match = array_values($match);
$controller(...$match);
};
// execute controller
echo 'foo route controller:';
$executeController($match1);
echo 'bar route controller:';
$executeController($match2);