0

I have a PHP project using XAMPP. And I try to debug with Xdebug plugin in Visual Studio Code.

The project about is a MVC framework. And on top of this framework I build a application.

So I have a folder controllers. And I have a folder Libraries within it the base controller.

But when I start Xdebug. I get this error:

Exception has occurred.
Fatal error: Uncaught Error: Class "Controller" not found in E:\Xampp\htdocs\shareposts\app\controllers\Users.php:2
Stack trace:
#0 {main}
  thrown

So I googled for this error and I found this: Fatal error: Class 'Controller' not found in php mvc

But that doesn't worked. And I can't of course put the Controller.php in the same folder as Users.php.

So this is the Users.php controller file:

<?php

require_once('Controller.php');
class Users extends Controller
{
  public function __construct()
  {
    $this->userModel = $this->model('User');
  }  
}
?>

So my question is: what I have to change?

So this is the controller class:

<?php
  /*
   * Base Controller
   * Loads the models and views
   */
  class Controller {
    // Load model
    public function model($model){
      // Require model file
      require_once '../app/models/' . $model . '.php';

      // Instatiate model
      return new $model();
    }

    // Load view
    public function view($view, $data = []){
      // Check for view file
      if(file_exists('../app/views/' . $view . '.php')){
        require_once '../app/views/' . $view . '.php';
      } else {
        // View does not exist
        die('View does not exist');
      }
    }
  }

and this is the bootstrap class. For initialising the files, when the project is starts:

<?php
  // Load Config
  require_once 'config/config.php';
  // Load Helpers
  require_once 'helpers/url_helper.php';
  require_once 'helpers/session_helper.php';

  // Autoload Core Libraries
  spl_autoload_register(function($className){
    require_once 'libraries/' . $className . '.php';
  });

and this is the launch.json file:

{
  // 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:8080([0-9]+)\\) started",
        "uriFormat": "http://localhost:8080%s",
        "action": "openExternally"
      }
    }
  ]
}

for example if I do this:

<?php

function test()
{
  $var = 1;
  $var++;
  return $var;
}

$hello = test();
echo $hello;

in a seperated project.

Then it works - I can debug the code.

12
  • I think we need to know more about Controller.php and if you're using name spacing, and if you're simply using class Controller { } within that file? Commented Mar 9, 2022 at 10:19
  • thoughtfulcode.com/a-complete-guide-to-php-namespaces Commented Mar 9, 2022 at 10:20
  • 1
    Is this solely a problem when XDebug is enabled? Commented Mar 9, 2022 at 10:24
  • 3
    It sounds to me that you are trying to debug your Users.php file directly and therefore bypassing the whole framework bootstrap logic (that's why PHP is unable to locate your base Controller class since the class autoloading code was not loaded). You should debug the URL instead -- so that the request goes through the whole framework including bootstrap code and then hits the breakpoint in your Users controller class. Commented Mar 9, 2022 at 10:58
  • 1
    github.com/xdebug/vscode-php-debug#vs-code-configuration . You need to use your Listen for Xdebug config and have debug.start_with_request=yes or pass the Xdebug cookie with the request (e.g. using Xdebug browser extension). Commented Mar 9, 2022 at 22:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.