-2

I just started to learn Symfony but I can't run my composer.

I am creating my composer using terminal php bin/console make:controller IndexController then im trying to run always getting this error:

Fatal error: Uncaught Error: Class "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" not found in C:\xampp\htdocs\my_project\src\Controller\IndexController.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\my_project\src\Controller\IndexController.php on line 9

and my composer code:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class IndexController extends AbstractController
{
    #[Route('/index', name: 'index')]
    public function index(): Response
    {
        return $this->render('index/index.html.twig', [
            'controller_name' => 'IndexController',
        ]);
    }
}

I searched errors like this but cant find anything helpful.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
adakakif
  • 1
  • 2
  • 3
  • 1
    Show how do you run it (the Run/Debug Configuration in PhpStorm). It looks like you are trying to execute this code directly and therefore bypassing the whole Symfony framework code including bootstrapping where Composer autoloader is used (that loads the classes for you in runtime). In such case the error is expected and correct. You need to run your whole app (or whatever entry point you may have for this) and not individual controller directly. – LazyOne Jun 07 '21 at 18:39
  • The fact that make:controller works implies that you do indeed have the complete framework installed and properly configured. So you can disregard the above comment and the answer below. Are you using the Symfony development server? I.E. did you run 'symfony server:start'. If you are trying to use the xamp web server then you might have some config issue with it. – Cerad Jun 07 '21 at 20:27
  • @Cerad Sure. Try executing controller file directly in a CLI and see what error you will get. – LazyOne Jun 07 '21 at 21:03
  • So you think they are actually trying to run: php src/Controller/IndexController.php? I'll admit that they would get the sort of error they posted. But in ten years of supporting Symfony I never came across someone trying to do that. Lets see if they come back or not. – Cerad Jun 07 '21 at 21:45
  • @Cerad Yes. I'm certain that this is the case here. In PhpStorm, if you right click and choose Run (or Debug) then Run/Debug Configuration of the ["PHP Script" type](https://www.jetbrains.com/help/phpstorm/run-debug-configuration-php-script.html) will be created. If person is new to frameworks (which OP is) and/or PhpStorm then it is quite easy to make such error and not realize what is going on / why it does not work. As an example (recent one): https://stackoverflow.com/questions/67721329/debugging-wordpress-with-xdebug-php-fatal-error-uncaught-error-call-to-undef – LazyOne Jun 07 '21 at 22:54

3 Answers3

1

This most probably mean that you class Symfony\Bundle\FrameworkBundle\Controller\AbstractController; does not exist for one of these reasons:

  • You forgot to launch composer install or composer update
  • You have the wrong path (check in you folder vendor if the file exist in vendor/symfony/framework-bundle/Controller/AbstractController.php
Florent Cardot
  • 1,400
  • 1
  • 10
  • 19
  • I found the AbstractController.php file this path : vendor/symfony/framework-bundle/Controller/AbstractController.php what do i need to do – adakakif Jun 07 '21 at 19:24
  • what do you do exactly to get this error? do you do something like ```php App\Controller\IndexController.php```? or do you right click on the file to launch it? or do you use your navigator with a specific url? – Florent Cardot Jun 08 '21 at 07:59
0

You maybe lost the [launch.json]file I think.

Summer
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 30 '22 at 17:45
0

You need to add a launch.json file like this:

  "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9000
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 0,
            "runtimeArgs": [
                "-dxdebug.start_with_request=yes"
            ],
            "env": {
                "XDEBUG_MODE": "debug,develop",
                "XDEBUG_CONFIG": "client_port=${port}"
            }
        },
        {
            "name": "Launch Built-in web server",
            "type": "php",
            "request": "launch",
            "runtimeArgs": [
                "-dxdebug.mode=debug",
                "-dxdebug.start_with_request=yes",
                "-S",
                "localhost:0"
            ],
            "program": "",
            "cwd": "${workspaceRoot}",
            "port": 9000,
            "serverReadyAction": {
                "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
                "uriFormat": "http://localhost:%s",
                "action": "openExternally"
            }
        }
    ]
}
Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
Summer
  • 1