Laravel 5.0,无法重新声明 class App\models\Category

Laravel 5.0, Cannot redeclare class App\models\Category

我最近将我的项目从 laravel 4.2 升级到 laravel 5.0,但遇到了一些错误。

我在 4.2 版本中没有定义任何命名空间,但是按照建议

我已经开始在我的代码中定义命名空间。我不知道我面临的问题是否与此有关,但它发生在这个过程的中间。

我在 运行 代码时遇到以下错误:

exception 'Symfony\Component\Debug\Exception\FatalErrorException' with
message 'Cannot redeclare class App\models\Category' in  
/Users/yash/summers/lightsCameraDinner/lcd_updated/app/models/Category.php:19

这是我的 Category.php:

<?php namespace App\models;

use Eloquent;

class Category extends Eloquent {

  protected $table = 'categories';
  protected $guarded = array('id');

  // Defining 'Many to Many' Relationship with 'VendorProfile' Model
  public function clients() {
    return $this->belongsToMany('Client');
  }

  // Defining 'One to Many' Relationship with 'Job' Model
  public function jobs() {
    return $this->hasMany('Job');
  }
}

我在SO上搜索了类似的错误,但没有找到。

这是我的控制器中在“/”路由上调用的函数。

    public function getIndex() {
    $categories = Category::all();

    $messages = Message::groupBy('receiver_id')
                ->select(['receiver_id', \DB::raw("COUNT('receiver_id') AS total")])
                ->orderBy('total', 'DESC')
                ->get()
                ->toArray();

    $vendors_ids = array();
    foreach ($messages as $message) {
      $vendors_ids[] = $message['receiver_id'];
    }

    $clients = Client::where('profile_type', 'VendorProfile')
                      ->where('is_activated', 1)
                      ->whereIn('id', $vendors_ids)
                      ->limit(4)
                      ->get();

    if($clients->count() < 4) {
      $clients = Client::where('profile_type', 'VendorProfile')
                        ->where('is_activated', 1)
                        ->limit(4)
                        ->get();
    }   
    Log::info('getIndex function of PagesController');
    $this->layout = View::make('layouts.homepage');
    $this->layout->content = View::make('pages/index', ['categories' => $categories, 'clients' => $clients]);
    return $this->layout;
  }

如果您需要代码中的任何其他内容,请告诉我。很长一段时间以来,我一直在努力寻找解决方案。

这是因为你生成了一个控制器,然后将它拖到一个子文件夹中。 您需要将命名空间更改为正确的命名空间或正确生成您的控制器。

php artisan make:controller Api/CategoryController  

或将命名空间更改为

namespace App\Http\Controllers\Api;

(如果 api 是控制器所在文件夹的名称)

我知道这个问题很老了,但我会回答对我有用的问题。 我最近在 git 分支上测试 Laravel 4.2 到 5.0 的升级。我在名为 Megaloquent 的模型 class 中遇到了同样的问题,它在 Laravel 4.2 中扩展了 Eloquent,现在是模型。

因为一开始我试图让它在没有命名空间的情况下工作,所以我在 composer.json

的 classmap 中添加了 app/ 子目录
"autoload": {
    "classmap": [
        "database",
        "app/Http/Controllers",
        "app/Http/Controllers/Auth",
        "app/Models",
        "app/Libraries"
    ],
    "psr-4": {
        "App\": "app/"
    }
},

在经历了很多麻烦之后,我决定在控制器和模型中使用命名空间,现在我发现它们更加结构化和精确。您应该从 classmap 中删除 app/Controllers-Models-Libraries,因为 psr-4 已经加载了 app/{subdirectories}/classes 中的所有 classes,并且 classmap autoload 让它发生两次。删除它们后你只会得到这个

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\": "app/"
    }
},

这是您无法真正控制的 Laravel 内部配置,但删除它们已为我修复了错误。