设置 Composer/自动加载我的类的问题

我正在学习 Composer 的工作方式(开发新手 ^^)但我正在努力修复我的自动加载...


这是我的 composer.json :


      "autoload": {

        "psr-4": {

            "OCFram\\": "/../lib/",

            "App\\": "/../",

            "Model\\": "/../lib/vendors/",

            "Entity\\": "/../lib/vendors/",

            "FormBuilder\\": "/../lib/vendors/",

            "Slug\\": "/../lib/vendors/"

        }

      },

例如:


致命错误:未捕获错误:找不到类“App\Frontend\FrontendApplication”


好吧,FrontendApplication 路径(来自 composer.json):**


../App/Frontend/FrontendApplication.php


这是带有命名空间的 FrontendApplication.php :


 <?php

 namespace App\Frontend;


 use \OCFram\Application;


 class FrontendApplication extends Application

 {

   public function __construct()

   {

      parent::__construct();


      $this->name = 'Frontend';

   } 


   public function run()

   {

      $controller = $this->getController();

      $controller->execute();


      $this->httpResponse->setPage($controller->page());

      $this->httpResponse->send();

   }

 }

另外,我注意到了 vendor/composer 上的这个文件 (autoload_psr4.php):


 <?php


// autoload_psr4.php @generated by Composer


 $vendorDir = dirname(dirname(__FILE__));

 $baseDir = dirname($vendorDir);


 return array(

     'Slug\\' => array('/lib/vendors'),

     'OCFram\\' => array('/lib'),

     'Model\\' => array('/lib/vendors'),

     'FormBuilder\\' => array('/lib/vendors'),

     'Entity\\' => array('/lib/vendors'),

     'App\\' => array('/'),

    );

希望得到一些帮助:)


青春有我
浏览 163回答 1
1回答

慕田峪7331174

根据你的说法:好吧,FrontendApplication 路径(来自 composer.json):**../App/Frontend/FrontendApplication.php您的文件夹结构似乎是:/App/<some-dir>/composer.json看起来你只是App在路径中错过了,你不需要前导或尾随斜线。&nbsp; &nbsp; &nbsp;"autoload": {&nbsp; &nbsp; &nbsp; &nbsp; "psr-4": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "OCFram\\": "../lib",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "App\\": "../App",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Model\\": "../lib/vendors",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Entity\\": "../lib/vendors",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "FormBuilder\\": "../lib/vendors",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Slug\\": "../lib/vendors"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; },
打开App,查看更多内容
随时随地看视频慕课网APP