0

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
Javi M
  • 97
  • 9
  • *"Is there any way to "simulate" an API call from the debugger console?"* Not using VSCode (PhpStorm user here) ... but you can use Postman or alike to call custom URLs with payload. Having Xdebug cookie as part of the request and using "Listen for Xdebug" VSCode config should do the job. On related note (PhpStorm, but overall approach is the same) https://stackoverflow.com/q/19139803/783119 – LazyOne Feb 03 '22 at 14:45
  • *"If I hit debug with the controller I'm interested in open on the editor, I get:"* That is correct - if you call a PHP script like that directly it will bypass all framework bootstrap code, especially your class autoloading. Such an error is 100% expected in such a case. You should use "Listen for Xdebug" in VSCode, set breakpoints where needed and control Xdebug "debug me" flag (a cookie) using browser's extension for Xdebug (when enabled it just sets the Xdebug cookie). You can also use `xdebug_break();` in your PHP code as a programmatic breakpoint. – LazyOne Feb 03 '22 at 14:49

1 Answers1

-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.

Bhimani Rutvik
  • 398
  • 2
  • 13
  • I just updated the question with the information you mention. Still the same situation. How do you manage the autoloads on the launch.json file? – Javi M Feb 03 '22 at 11:17