简体   繁体   中英

Composer autoload can't find class

I'm trying to create a MVC structure and use composer to autoload everything.
But I keep getting this error:

Fatal error: Uncaught Error: Class 'App\\Init' not found in C:\\wamp64\\www\\activity\\Public\\index.php on line 5

|MainFolder  
    |App  
    |Public  
    |Vendor  
        |ACT  
        |composer  
        |autoload.php  
    |composer.json  

composer.json:

{
    "name": "vendor/activity",
    "description": "descrip",

    "require": {
        "php": ">=5.6.25"
    },

    "authors":[
        {
            "name": "John Doe",
            "email": "johndoe@gmail.com"
        }
    ],

    "autoload":{
        "psr-4": {
            "ACT\\": "vendor/",
            "App\\": "/"
        }
    },
    "config":{
        "bin-dir": "bin"
    }
}

App\\init.php

<?php    
namespace App;

class Init
{
    public function __construct()
    {
        echo "Loaded!!";
    }
}

Public\\index.php

<?php
require_once '../vendor/autoload.php';

$init = new \App\Init;  

\\Vendor\\composer\\autoload_namespaces.php

<?php    

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    "ACT" => array($vendorDir . false),
    "App" => array($baseDir . '/'),
);  

Obs: Already did composer dump-autoload

  1. Don't manually put things in /vendor .
  2. While adhering to #1, don't reference /vendor in autoload , the packages should all have their own fully-functionaly autoloaders that composer will find and use.
  3. You need to specify more of the path in your autoload.

"autoload":{
    "psr-4": {
        "App\\": "App/"
    }
},

Think of it like telling composer "look for things starting with the namespace foo\\bar\\ in the following folder".

Note : The folder name doesn't have to match the namespace.

Eg: Following the suggested Vendor\\Package\\ scheme for PSR/Composer

{
  "autoload": {
    "psr-4": {
      "sammitch\\meatstacker\\": "src/"
    }
  }
}

And then:

  • \\sammitch\\meatstacker\\Client maps to src/Client.php
  • \\sammitch\\meatstacker\\Bread\\Rye maps to src/Bread/Rye.php
  • and so on

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