'How to debug CodeIgniter with VSCode in localhost?

I am trying to debug my CodeIgniter app on my localhost. I have set up the up the DB connection and I can access the services from the browser:

URL: http://localhost/api/are_you_alive
Resp: Yes

I have installed PHP Debug following the suggestions from here and here, but I still cannot debug my REST API.

I am trying to debug a method inside a controller under /app/Controllers/MyController.php.

 public function index(){
    echo "Hi";
  }

As the framework has to load all the configuration before reach the default controller, I have started the debugger on /public/index.php, otherwise I get the following error:

PHP Fatal error:  Uncaught Error: Class 'App\Controllers\BaseApi' not found in /var/www/html/my_project/app/Controllers/Api.php:5

If I hit "run" WITHOUT breakpoints, all goes well. As expected. But when I set a breakpoint anywhere, the debugger enter into an "infinite" loop and it never reach my breakpoint neither stops.

  • How can I debug a REST API made with CodeIgniter using VSCode?
  • Is there any way to "simulate" an API call from the debugger console?

Thanks

UPDATE --------------------

My launch.json file is:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003
        },
        {
            "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": 9003,
            "serverReadyAction": {
                "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
                "uriFormat": "http://localhost:%s",
                "action": "openExternally"
            }
        }
    ]
}

If I hit debug with the controller I'm interested in open on the editor, I get:

PHP Fatal error:  Uncaught Error: Class 'App\Controllers\BaseApi' not found in /var/www/html/bases/back/app/Controllers/Api.php:5
Stack trace:
#0 {main}
  thrown in /var/www/html/bases/back/app/Controllers/Api.php on line 5


Solution 1:[1]

Check you launch.json is proper or not. See my lanch JSON.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Listen for Xdebug 2 (Legacy)",
            "type": "php",
            "request": "launch",
            "port": 9000
        },


    ]
}

You also need to check autoloads.Open that view that you want to debug then start debugger in vs code and perform an action that triggers your break point.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bhimani Rutvik