Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

TypeError in Router when using UrlGenerator #47844

Copy link
Copy link
Closed
@dbroeks

Description

@dbroeks
Issue body actions

Symfony version(s) affected

5.4 and 6.x

Description

Since Symfony 5.4, the Symfony\Component\Routing\Router's getGenerator method merges an array of routes with an array of aliases. In development environments, we like to use the Symfony\Component\Routing\Generator\UrlGenerator, which works with a Symfony\Component\Routing\RouteCollection object instead of an array. array_merge() can not deal with such an object, but does get passed one, resulting in a TypeError. This does not happen with the Symfony\Component\Routing\Generator\CompiledUrlGenerator.

How to reproduce

I created a tiny project (php 8.0) with our basic setup and the minimum amount of code to prove the issue. When run with Symfony 5.3, it prints a slash, which is the correctly generated url. From 5.4 onwards, a TypeError is thrown. It can simply be executed from the CLI: php routing.php

routing.php:

<?php

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Loader\GlobFileLoader;
use Symfony\Component\Routing\Loader\PhpFileLoader;
use Symfony\Component\Routing\Router;

require __DIR__ . '/vendor/autoload.php';

$fileLocator = new FileLocator([__DIR__]);
$loader = new GlobFileLoader($fileLocator);
$loader->setResolver(new LoaderResolver([new PhpFileLoader($fileLocator)]));

$router = new Router(
    loader: $loader,
    resource: 'routes/*.php',
    options: [
        'generator_class' => UrlGenerator::class,
        'cache_dir' => null
    ]
);

echo $router->generate('home') . PHP_EOL;

routes/routes.php:

<?php

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

$collection = new RouteCollection();
$collection->add('home', new Route('/'));
return $collection;

composer.json:

{
    "require": {
        "symfony/routing": "5.4.*",
        "symfony/config": "5.4.*"
    }
}

Possible Solution

No response

Additional Context

PHP Fatal error:  Uncaught TypeError: array_merge(): Argument #1 must be of type array, Symfony\Component\Routing\RouteCollection given in /redacted/vendor/symfony/routing/Router.php:323
Stack trace:
#0 /redacted/vendor/symfony/routing/Router.php(323): array_merge(Object(Symfony\Component\Routing\RouteCollection), Array)
#1 /redacted/vendor/symfony/routing/Router.php(235): Symfony\Component\Routing\Router->getGenerator()
#2 /redacted/routing.php(25): Symfony\Component\Routing\Router->generate('home')
#3 {main}
  thrown in /redacted/vendor/symfony/routing/Router.php on line 323

Fatal error: Uncaught TypeError: array_merge(): Argument #1 must be of type array, Symfony\Component\Routing\RouteCollection given in /redacted/vendor/symfony/routing/Router.php:323
Stack trace:
#0 /redacted/vendor/symfony/routing/Router.php(323): array_merge(Object(Symfony\Component\Routing\RouteCollection), Array)
#1 /redacted/vendor/symfony/routing/Router.php(235): Symfony\Component\Routing\Router->getGenerator()
#2 /redacted/routing.php(25): Symfony\Component\Routing\Router->generate('home')
#3 {main}
  thrown in /redacted/vendor/symfony/routing/Router.php on line 323

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.