SymfonyModularRouting

[DEPRECATED] Decouple your Symfony routing to independent, standalone services.

MIT License

Downloads
6.9K
Stars
17
Committers
1

Modular Routing

To add routes you usually need to add few lines to app/config/routing.yml. If you have over dozens of modules, it would be easy to get lost in it. To see all options on how to do that including this package, read this short article.

Thanks to this router, you can add them easily as via service loader.

Install

composer require symplify/modular-routing

Add bundle to AppKernel.php:

final class AppKernel extends Kernel
{
    public function registerBundles(): array
    {
        $bundles = [
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle(),
            new Symplify\ModularRouting\SymplifyModularRoutingBundle(),
            // ...
        ];
    }
}

Usage

  1. Implement RouteCollectionProviderInterface

    use Symfony\Component\Routing\Route;
    use Symfony\Component\Routing\RouteCollection;
    use Symplify\ModularRouting\Contract\Routing\RouteCollectionProviderInterface;
    
    final class SomeRouteCollectionProvider implements RouteCollectionProviderInterface
    {
        public function getRouteCollection() : RouteCollection
        {
            $routeCollection = new RouteCollection();
            $routeCollection->add('my_route', new Route('/hello'));
    
            return $routeCollection;
        }
    }
    
  2. Register service

    services:
        some_module.route_provider:
            class: SomeModule\Routing\SomeRouteCollectionProvider
            autowire: true # or better use Symplify\DefaultAutowire package
    

That's all!

Loading YML/XML files

In case you want to load these files, just use AbstractRouteCollectionProvider with helper methods.

use Symfony\Component\Routing\RouteCollection;
use Symplify\ModularRouting\Routing\AbstractRouteCollectionProvider;

final class FilesRouteCollectionProvider extends AbstractRouteCollectionProvider
{
    public function getRouteCollection(): RouteCollection
    {
        return $this->loadRouteCollectionFromFiles([
            __DIR__ . '/routes.xml',
            __DIR__ . '/routes.yml',
        ]);
        
        // on in case you have only 1 file
        // return $this->loadRouteCollectionFromFile(__DIR__ . '/routes.yml');
    }
}

Contributing

Send issue or pull-request to main repository.