简体   繁体   中英

PHP autoload can't find the class in php project

Project file structure I'm getting

 127.0.0.1:52368 [500]: GET /controllers/login - Uncaught Error: Class "App\Core\Database" not found in /path/to/my/Php/project/src/controllers/index.php:9
Stack trace:
#0 {main}
  thrown in  /path/to/my/Php/project/src/controllers/index.php on line 9

and the class firing an error

<?php

namespace App\controllers;

use App\Core\Database;

//$conn = Database::getConnection();
try {
    $conn = new Database(__DIR__ . './../config.php');
} catch (\Exception $e) {
}
$query = "SELECT id, username, image, aboutme
            FROM users
                 LEFT JOIN persons on users.id = persons.user_id
            LIMIT 24";
$tbh = $conn->prepare($query);
$tbh->execute();
$persons = $tbh->fetchAll();
require_once basename("/") . 'views/index.view.php';

also my autoload_classmap.php

return array(
    'App\\Core\\Database' => $baseDir . '/src/Core/Database.php',
    'App\\Core\\DotEnv' => $baseDir . '/src/Core/DotEnv.php',
    'App\\Core\\Router' => $baseDir . '/src/Core/Router.php',
    'App\\Entity\\Person' => $baseDir . '/src/Entity/Person.php',
    'App\\Entity\\User' => $baseDir . '/src/Entity/User.php',

And I'm launching server from src directory: php -S localhost:8000 Any help would be appreciated. Thank you.

Edit... I'm calling index.php from url: localhost:8000 and even with this code in index.php

    <?php
    namespace App;
    
    //use App\core\Database;
    use App\Core\Router;
    
    require 'functions.php';
    
    //$config = require_once ('config.php');
    //$db = new Database($config);
    
    //require 'Entity/User.php';
    
    $uri = parse_url($_SERVER['REQUEST_URI'])['path'];
    
    $router = new Router();
    $router->getRoute($uri);
    
    
    //require 'router.php';
I'm getting 
127.0.0.1:53738 [500]: GET / - Uncaught Error: Class "App\Core\Router" not found in

You must create a class and add data inside it.

prepare($query); $tbh->execute(); $persons = $tbh->fetchAll(); require_once basename("/"). 'views/index.view.php'; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM