How to upload a route from several files?

Need to download the route directly from files in different directories.
In the documentation there is an example that shows how to search for a file in multiple directories in one file and then get from there the route:
$fileLocator = new FileLocator(array(__DIR__));
$loader = new YamlFileLoader($fileLocator);
$routes = $loader->load('routes.yaml');


What to do to get the files from different directories, and then to work with them?
Arranged like this:
data/
├── dir1
│ └── routes.yaml
├── dir2
│ └── routes.yaml
...

Ie you need to take all the files in the routes.yaml in the specified folder, and there already to search for the route

Whether really such to make?
April 7th 20 at 11:00
1 answer
April 7th 20 at 11:02
Solution
If you know the location of the configuration, it is possible so:
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\RouteCollectionBuilder;

require_once __DIR__.'/../vendor/autoload.php';

$locator = new FileLocator([__DIR__.'/../data']);
$loader = new YamlFileLoader($locator);

$builder = new RouteCollectionBuilder($loader);
$builder->import('dir1/routes.yaml');
$builder->import('dir2/routes.yaml');

$routes = $builder->build();
var_dump($routes);


If the location configuration is unknown or you just don't want to bother adding new:
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Routing\Loader\GlobFileLoader;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\RouteCollectionBuilder;

require_once __DIR__.'/../vendor/autoload.php';

$locator = new FileLocator([__DIR__.'/../data']);
$resolver = new LoaderResolver([
 new GlobFileLoader($locator), // needs symfony/finder
 new YamlFileLoader($locator),
]);
$loader = new DelegatingLoader($resolver);

$builder = new RouteCollectionBuilder($loader);
$builder->import('**/*/routes.yaml', '/', 'glob');

$routes = $builder->build();
var_dump($routes);

Find more questions by tags Symfony