Reputation: 29
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!
Upvotes: 1
Views: 2244
Reputation: 470
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.
Upvotes: 0
Reputation: 29
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
}
}
}
Upvotes: 1