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.
Controller.phpand if you're using name spacing, and if you're simply usingclass Controller { }within that file?Users.phpfile directly and therefore bypassing the whole framework bootstrap logic (that's why PHP is unable to locate your baseControllerclass 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 yourUserscontroller class.Listen for Xdebugconfig and havedebug.start_with_request=yesor pass the Xdebug cookie with the request (e.g. using Xdebug browser extension).