Ankündigung

Einklappen
Keine Ankündigung bisher.

Class not found mit verschachtelter Struktur

Einklappen

Neue Werbung 2019

Einklappen
Dieses Thema ist geschlossen.
X
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Class not found mit verschachtelter Struktur

    Hallo zusammen

    Der Fehler passiert auf Zeile 30 im index.php (new Post\PostController...) -> Class 'App\Post\PostController' not found in .../index.php. Beim Aufruf von get_declared_classes() in autoload.php nachdem die Klasse hinzugefügt wurde, wird App\Post\PostController als Klasse im Array aufgelistet. Trotzdem erhält man den oben genannten Fehler mit derselben Klassensignatur.

    Datenstruktur ist folgende:

    index.php
    src/Post/ -> PostController.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;
    }


     
    ?>
    PostController.php
    PHP-Code:
    namespace App\Post;

    class 
    PostController {...} 
    init.php
    PHP-Code:
    namespace App;

    echo 
    'autoload will load';
    require 
    __DIR__.'/autoload.php';
    require 
    __DIR__.'/dbconn.php'
    autoload.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);
        }
    }); 

    Für jede Hilfe von einem Profi wäre ich sehr dankbar, habe schon stundenlang daran herum probiert

  • #2
    Hast Du den Kram als Download? Hab grad keine Lust den Mist einzuhacken. Fehler hab ich schon gefunden.

    Kommentar


    • #3
      https://www.php-resource.de/forum/ph...not-found.html

      [MOD: Wegen Crosspost geschlossen.]

      Kommentar

      Lädt...
      X