0
votes

Following the example in the documentation:

https://symfony.com/doc/current/page_creation.html

I run into this error message:

The autoloader expected class "App\Controller\LuckyController" to be defined in file "/var/www/my-project/vendor/composer/../.. /src/Controller/LuckyController.php". The file was found but the class was not in it, the class name or namespace probably has a typo in /var/www/my-project/config/services.yaml (which is loaded in resource "/var/www/my-project/config/services.yaml").

In the "src/Controller/LuckyController.php", I have:

namespace App\Controller;  
use Symfony\Component\HttpFoundation\Response;  
class LuckyController {  
  public function number() {   
    $number = mt_rand(0, 100);  
    return new Response('<html><body>Lucky number: '.$number.'</body></html>');  
  }  
}

In the service.yml, I have:

parameters:

services:
    _defaults:
        autowire: true      
        autoconfigure: true 
        public: false       

    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests}'

    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

I am not sure why the class "App\Controller\LuckyController" cannot be found. I wonder if anyone can help. Many thanks!

2
Do you have <?php at the top of your controller file? - Cerad
I did not. After I add this to the controller file. It works. Many thanks!! - CN Li
Hint: Next time you have a question start by pulling out something unique like "LuckyController" and pasting it into the search bar. You might be suprised at what you find. - Cerad
Can you share your composer.json ? - albert

2 Answers

1
votes

After I add '<?php' to the top of the 'LuckyController.php', it works.

About Albert's comment, here is the composer.json I have. Please note, when I follow along the example in the documentation, this file seems to be added and modified by 'composer require'. So far, I have not manually edited it myself.

{ "type": "project", "license": "proprietary", "require": { "php": "^7.1.3", "ext-iconv": "*", "sensio/framework-extra-bundle": "^5.1", "sensiolabs/security-checker": "^4.1", "symfony/apache-pack": "^1.0", "symfony/console": "^4.0", "symfonby/flex": "^1.0", "symfony/framework-bundle": "^4.0", "symfony/lts": "^4@dev", "symfony/requirements-checker": "^1.0", "symfony/twig-bundle": "^4.0", "symfony/yaml": "^4.0" }, "require-dev": { "symfony/dotenv": "^4.0", "symfony/profiler-pack": "^1.0", "symfony/thanks": "^1.0", "symfony/web-server-bundle": "^4.0" }, "config": { "preferred-install": { "*": "dist" }, "sort-packages": true }, "autoload": { "psr-4": { "App\\": "src/" } }, "autoload-dev": { "psr-4": { "App\\Tests\\": "tests/" } }, "replace": { "symfony/polyfill-iconv": "*", "symfony/polyfill-php71": "*", "symfony/polyfill-php70": "*", "symfony/polyfill-php56": "*" }, "scripts": { "auto-scripts": { "cache:clear": "symfony-cmd", "assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd", "requirements-checker": "script", "security-checker security:check": "script" }, "post-install-cmd": [ "@auto-scripts" ], "post-update-cmd": [ "@auto-scripts" ] }, "conflict": { "symfony/symfony": "*" }, "extra": { "symfony": { "id": "...", "allow-contrib": false } } }

0
votes

This error can be caused by any syntactic error in the definitions of the class. I spent half an hour to debug it in a case similar to the following:

<?php

namespace App\MyServices;

class MyClass
{
   private $firstAttr;
   private $secondAttr
   private $thirdAttr;

   public function foo($bar)
   {
      // ...
   }
}

What you have to spot here is the missing semicolon after the definition of $secondAttr. If the namespace and filename are correct, check that this is also the case for interfaces, parent classes, and traits. And double check for this kind of syntactic errors, which can also be "imported" from traits.