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
Requires
- php: ^7.1
- symfony/dependency-injection: ^3.3|^4.0
- symfony/framework-bundle: ^3.3|^4.0
- symfony/http-kernel: ^3.3|^4.0
- symfony/routing: ^3.3|^4.0
- symfony/twig-bundle: ^3.3|^4.0
- symfony/yaml: ^3.3|^4.0
This package is auto-updated.
Last update: 2024-11-04 21:17:26 UTC
README
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
-
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; } }
-
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'); } }