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

设置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"
            }
          },
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题