php - Cannot autoload routing from Bundles ← (PHP, Symfony, HTML)

I have a problem wuth autoloading routing from bundles. I have routing.yml file in folder: Bundle\UsersBundle\Resources\config. I am loading routes as they say in Symfony cookbook(http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html). And it all works great with one bundle but when I add another bundle to AppKernel it does not load new routing. It looks like every bundle needs separate entry in main routing.yml(which is exactly what I don't want to have). Every RoutesLoader service has set right tags and container is loading this services properly.

routing.yml: (this config works but it needs that RouteLoader from another bundle have to return another type - "bundle_routes_2" - when it returns "bundle_routes" routing is not loaded)

bundles_routes:
    resource: .
    type: bundle_routes

bundles_routes_2:
    resource: .
    type: bundle_routes_2

To be more specific - for example I have 3 bundles - UsersBundle, PermissionsBundle and GroupsBundle. Every bundle has routing in its Resources folder(bundle_folder/Resources/config/routing.yml). Every bundle has RoutesLoader class which look like that:

<?php

namespace Acme\UsersBundle\Routing;

use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\RouteCollection;

class UsersRoutesLoader extends Loader
{
    public function load($resource, $type = null)
    {
        $collection = new RouteCollection();
        $resource = "@AcmeUsersBundle/Resources/config/routing.yml";
        $type = "yaml";
        $importedRoutes = $this->import($resource, $type);
        $collection->addCollection($importedRoutes);
        return $collection;
    }

    public function supports($resource, $type = null)
    {
        return $type === "bundle_routes";
    }
}

And main routing file looks lik that:

acme_bundles_routes:
    resource: .
    type: bundle_routes

But it works only for one bundle. I have to change routing.yml to:

acme_bundles_routes:
        resource: .
        type: bundle_routes
acme_bundles_routes_2:
        resource: .
        type: bundle_routes_2
acme_bundles_routes_3:
        resource: .
        type: bundle_routes_3

And every RoutesLoader have to return correspondig type.

Answer



Solution:

Why do you need a custom loader?

See http://symfony.com/doc/current/book/routing.html#routing-include-external-resources

Your parent routing.yml should contain:

AcmeBundle:
    resource: "@AcmeBundle/Resources/config/routing.yml"

Since you want to have various routing files, you can then have (in routing.yml):

admin_routes:
    resource: "@AcmeBundle/Resource/config/routes/admin.yml"
user_routes:
    resource: "@AcmeBundle/Resource/config/routes/user.yml"

And so on and so forth. Inside any of these bundle yml files you'd contain your path definitions.

Source