0

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?

4
  • Start by deleting the cache directly completely. It's possible that this is hold over from some of your earlier attempts. Otherwise, just grep your entire project for restful_resource. It's an unusual name and is coming from someplace. If you are using S3.3 then autowire might be messing you up. Try adding Resources to the excluded directories. Commented Sep 7, 2017 at 14:55
  • Sorry, I forgot to include in the question that restful_resource.php is the name of file generated based on the entity. I'll try clearing the cache. Commented Sep 7, 2017 at 14:59
  • 1
    Okay. It is almost certainly autowire messing you up. Out of the box S3.3+ tries to make everything a service. So edit app/config/services.yml and add Resources to the exclude directory list. Commented Sep 7, 2017 at 15:01
  • Yep, looks like that was it. I added the Resources directory to be excluded from autowiring and everything went smoothly. Would you like to post your comment as an answer for the credit? Commented Sep 7, 2017 at 15:28

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.