Shaun
Shaun

Reputation: 2052

Symfony using yaml and php routing

I would like to use a mix of yaml and php routing in a Symfony (3.3.8) app. I am pretty comfortable with yaml routing, so I used the bin/console doctrine:generate:crud command to see what PHP routing would look like. It generated a routing file that looks like

<?php

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

$collection = new RouteCollection();

$collection->add('user_index', new Route(
    '/',
    array('_controller' => 'AppBundle:User:index'),
    array(),
    array(),
    '',
    array(),
    array('GET')
));

// other CRUD routes...

return $collection;

This follows the format suggested on the Symfony docs, where you build up a RouteCollection with Routes and then return it.

When I try to run my app with this file in place, even if it is not referenced from my main routing.yml file, I get this error:

 [Symfony\Component\Config\Exception\FileLoaderLoadException]
  The autoloader expected class "AppBundle\Resources\config\routing\restful_resource" to be defined i
  n file "/home/username/sites/appname/vendor/composer/../../src/AppBundle/Resources/config/routing/restfu
  l_resource.php". The file was found but the class was not in it, the class name or namespace probab
  ly has a typo in /home/username/sites/appname/app/config/services.yml (which is being imported from "/ho
  me/username/sites/appname/app/config/config.yml").



  [RuntimeException]
  The autoloader expected class "AppBundle\Resources\config\routing\restful_resource" to be defined i
  n file "/home/username/sites/appname/vendor/composer/../../src/AppBundle/Resources/config/routing/restfu
  l_resource.php". The file was found but the class was not in it, the class name or namespace probab
  ly has a typo.

Do I need to redesign this file to act like a class, going against the suggested format in the Symfony docs? Or do I need to somehow tell the autoloader to ignore this file so it doesn't try to find a class where there shouldn't be one?

Upvotes: 0

Views: 646

Answers (1)

Cerad
Cerad

Reputation: 48865

One of the big changes introduced in Symfony 3.3 was the notion of automatically wiring up services. In my not so humble opinion, lots and lots of inconsistent magic for little benefit.

As part of the auto wiring process, it was decided to make every class a service by default. Any php file is assumed to have a class in it. Hence the attempt to find a class in Resources/config/routing/restful_resource.php.

To prevent this behavior, you need to explicitly tell the service generator to skip directories.

// app/config/services.yml
AppBundle\:
    resource: '../../src/AppBundle/*'
    exclude: '../../src/AppBundle/{Entity,Repository,Tests,Resources}'

The good thing about introducing all these new "helper" functions is that I get to earn quite a bit of rep explaining them.

Upvotes: 2

Related Questions