netbull/routing-bundle

Load your Symfony routes with a simple service.

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:symfony-bundle

dev-master 2019-01-04 08:35 UTC

This package is auto-updated.

Last update: 2024-11-04 21:17:26 UTC


README

Build Status

To add routes you usually need to add few lines to config/routing.yml.

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

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require netbull/routing-bundle

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new NetBull\RoutingBundle\NetBullRoutingBundle(),
        );

        // ...
    }

    // ...
}

Usage

  1. Implement RouteCollectionProviderInterface

    use Symfony\Component\Routing\Route;
    use Symfony\Component\Routing\RouteCollection;
    use NetBull\RoutingBundle\Routing\RouteCollectionProviderInterface;
    
    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

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 NetBull\RoutingBundle\Routing\AbstractRouteCollectionProvider;

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');
    }
}