Home Ask Login Register

Developers Planet

Your answer is one click away!

Zend February 2016

Create custom router service

am creating own framework based on Sf2 commponents and i try to create router service.

I i need that service for generateUrl() method

protected function generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
    return $this->get('router')->generate($route, $parameters, $referenceType);

I try this

$container = new ContainerBuilder();

$container->setDefinition('router_loader', new Definition('Symfony\Component\Config\Loader\LoaderInterface'));
$container->setDefinition('router', new Definition('Symfony\Component\Routing\Router', array()));

And when i execute in my methodAction


he return me:

Catchable fatal error: Argument 1 passed to Symfony\Component\Routing\Router::__construct() must be an instance of Symfony\Component\Config\Loader\LoaderInterface, none given in D:\xampp\htdocs\my_fw\vendor\symfony\routing\Router.php on line 95

looking on router constructor i see. I need that interface

   public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, LoggerInterface $logger = null)

how to avoid that implementation in service?

**New update:** routing.php

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

// Routing
$routes = new RouteCollection();

// Home

$routes->add('home', new Route('/', array(
    '_controller' => 'MyCompany\\Controller\\HomeController::indexAction',


elkorchi anas February 2016

You are having this error because you are configuring the router service with no service definition the service definition should have your service arguments which is the source of your error because the container try to create the router with no arguments check this

For better use you can configure the router service in the service.yml/.xml file

chh February 2016

Edit: The documentation page for the Routing component has detailed setup instructions

Try injecting the router_loader service as an argument into the router service. For this case you have to use the Symfony\Component\DependencyInjection\Reference class.

Routes have to be configured with a config file when using the all in one Router class. You have to use a FileLocator and a real Loader class, like the YamlFileLoader, you can't just use the interface (services generally can't be interfaces in Symfony).

The service container setup for the router service should look like this:

use Symfony\Component\DependencyInjection\Reference;

// Loads config files from the current directory, change this to 
// your liking, or add more than one path
$container->setDefinition('router_config_locator', new Definition(
    'Symfony\Component\Config\FileLocator', [[__DIR__]]

$container->setDefinition('router_loader', new Definition(
    'Symfony\Component\DependencyInjection\Loader\YamlFileLoader', [
        new Reference('router_config_locator'),

     new Definition('Symfony\Component\Routing\Router', array(
         'loader' => new Reference('router_loader'),
         // Definition of routes in Yaml form
         'resource' => 'routes.yml',

The routes.yml file contains your route definitions:

  path: /home
  defaults: {_controller: "MyController:index"}

Also have a look at this documentation page about setting up the routing component, which also talks about setting up

Post Status

Asked in February 2016
Viewed 2,449 times
Voted 5
Answered 2 times


Leave an answer

Quote of the day: live life