【问题标题】:Laravel could not autoload a model from within Acme Directory?Laravel 无法从 Acme 目录中自动加载模型?
【发布时间】:2014-04-27 16:09:40
【问题描述】:

在我的 Laravel 应用程序中,我在应用程序目录中创建了 Acme\fooDir 目录

我正在尝试从 fooDir 中调用模型 -“barModel”

 //app/Acme/fooDir/test.php 
 $barM = new barModel;

但我收到此错误

        Class 'Acme\fooDir\barModel' not found

这是我的应用结构

  app
  -app/Acme
  --app/Acme/fooDir
  ---app/Acme/fooDir/test.php (this is the file that could load the barModel)
  -app/models
  --app/model/barModel.php (this is the model i am trying to use)

我在 composer.json 中添加了自动加载

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ],
    "psr-0": 
    { 
        "Acme": "app/"           
    }

和 我已经运行了命令

composer dump-autoload -o

php artisan dump-autoload

但问题没有解决,我仍然遇到同样的错误

    Class 'Acme\fooDir\barModel' not found

有什么想法吗?

【问题讨论】:

    标签: php laravel laravel-4 composer-php


    【解决方案1】:

    使用namespace 时,您的所有类都在namespace 内调用。

    如果您想在Acme/fooDir/test.php 中使用barModel,则需要在namespace 行之后使用use barModel;

    <?php
    namespace Acme\fooDir;
    
    use barModel;
    
    class test {
    
    }
    

    【讨论】:

      【解决方案2】:

      两件事。

      1,你有没有命名你的类,即,

      <?php namespace Acme\FooDir;
      
      class Test ...
      

      2,你在课堂上使用 use 吗

      <?php namespace Acme\FooDir;
      
      use \BarModel
      
      class Test ...
      

      ?

      【讨论】:

        【解决方案3】:

        这对你有用吗?

         $barM = new \barModel;
        

        PHP namespaces and importing

        【讨论】:

          猜你喜欢
          • 2015-04-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-11
          • 1970-01-01
          • 2014-07-18
          • 1970-01-01
          相关资源
          最近更新 更多