drz5553 2019-05-24 14:01
浏览 141
已采纳

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

I'm learning how Composer is working (new to dev ^^) But i'm struggling to fix my autoloading...

here's my composer.json :

      "autoload": {
        "psr-4": {
            "OCFram\\": "/../lib/",
            "App\\": "/../",
            "Model\\": "/../lib/vendors/",
            "Entity\\": "/../lib/vendors/",
            "FormBuilder\\": "/../lib/vendors/",
            "Slug\\": "/../lib/vendors/"
        }
      },

So for example :

Fatal error: Uncaught Error: Class 'App\Frontend\FrontendApplication' not found

Well, FrontendApplication path (from composer.json) : **

../App/Frontend/FrontendApplication.php

Here's the FrontendApplication.php with the namespace :

 <?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();
   }
 }

Plus, i've , noticed this file (autoload_psr4.php) on vendor/composer :

 <?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('/'),
    );

Would appreciate some help :)

[EDIT]

So i changed the path from "App\": "/../" (which was no sense) to : "App\": "../",

NOW after another composer dump-autoload i get this :

                 <?php

                 // autoload_psr4.php @generated by Composer

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

                return array(
                'Slug\\' => array($baseDir . '/../lib/vendors'),
                'OCFram\\' => array($baseDir . '/../lib'),
                'Model\\' => array($baseDir . '/../lib/vendors'),
                'FormBuilder\\' => array($baseDir . '/../lib/vendors'),
                'Entity\\' => array($baseDir . '/../lib/vendors'),
                'App\\' => array($baseDir . '/..'),
                );

But still same problem when i try php index.php i get : Fatal error: Uncaught Error: Class 'App\Frontend\FrontendApplication' not found

  • 写回答

1条回答 默认 最新

  • drwkqwa82140 2019-05-24 14:16
    关注

    As of your statement:

    Well, FrontendApplication path (from composer.json) : **

    ../App/Frontend/FrontendApplication.php

    You folder structure seems to be:

    /App
    /<some-dir>/composer.json
    

    It seems you just missed App in the path, you don't need leading or trailing slashes.

         "autoload": {
            "psr-4": {
                "OCFram\\": "../lib",
                "App\\": "../App",
                "Model\\": "../lib/vendors",
                "Entity\\": "../lib/vendors",
                "FormBuilder\\": "../lib/vendors",
                "Slug\\": "../lib/vendors"
            }
          },
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器