Fehler passiert auf Zeile 30 im index.php -> Class 'App\Post\PostController' not found in .../index.php. Kurioserweise beim Aufruf von get_declared_classes() in autoload.php gleich umittelbar nach implizitem Aufruf der Funktion wird App\Post\PostController als Klasse im Array aufgelistet
Datenstruktur ist folgende:
index.php
src/Post/ -> PostController.php, PostGateway.php
/startup/ -> autoload.php, dbconn.php, init.php
index.php
PostController.php
init.php
autoload.php
Datenstruktur ist folgende:
index.php
src/Post/ -> PostController.php, PostGateway.php
/startup/ -> autoload.php, dbconn.php, init.php
index.php
PHP Code:
<?php
namespace App;
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=UTF-8');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Max-Age: 3600');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
include_once('./startup/init.php');
//var_dump(new Post\PostController());
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri_parts = explode('/', $uri);
$requestMethod = $_SERVER['REQUEST_METHOD'];
$ressourceClass = $uri_parts[1];
$id = null;
if(isset($uri_parts[2])){
$id = (int) $uri_parts[2];
}
switch ($ressourceClass) {
case 'posts':
$controller = new Post\PostController($dbconn, $requestMethod, $id);
$controller->processRequest();
break;
default:
header('HTTP/1.1 404 Not Found');
exit();
// break;
}
?>
PHP Code:
namespace App\Post;
class PostController****{...}
PHP Code:
namespace App;
echo 'autoload will load';
require __DIR__.'/autoload.php';
require __DIR__.'/dbconn.php';
PHP Code:
spl_autoload_register(function ($class) {
// project-specific namespace prefix
$prefix = 'App\\';
// base directory for the namespace prefix
$base_dir = dirname(dirname(__FILE__)) . '/src/';
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
print 'File: '.$file;
// if the file exists, require it
if (file_exists($file)) {
require $file;
print_r(get_declared_classes());
$controller = new App\Post\PostController(null, 'GET', 1);
}
});
Comment