【问题标题】:Laravel 5 Fatal Error: class not found exception cannot access the model classes when it is inside a folderLaravel 5致命错误:找不到类异常在文件夹内时无法访问模型类
【发布时间】:2015-08-21 18:43:48
【问题描述】:

这是我的应用程序结构,

my-first-app
   |    
   |-app
     |
     | -Commands
     | -Console
     | -Events
     | -Http
     | -models(custom define folder)
        |
        |-Base(folder)
        |   |
        |   | -StuDetails.php
        |   | -StuDetailsQuery.php
        |
        |-Map(foder)
        |-StuDetails.php
        |-StuDetailsQuery.php

实际上我正在使用 propel ORM 从数据库中检索数据。 我在文件夹 models 中创建了我的模型类。 然后我尝试访问我的 pageController 类中的 StudetailsQuery 类,该类位于扩展为 Controller 的 http 文件夹中。

这里是:

<?php
namespace App\models\propel;
namespace App\Http\Controllers;


use App\Http\Requests;
use App\Http\Controllers\Controller;


use Base\StuDetailsQuery;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;

class pagesController extends Controller
{


    public function index()
    {
        $stu= StuDetailsQuery::create()->find();
        return view('pages.index', compact('stu'));
    } 

但我得到错误:

PHP 致命错误:类 'Base\StuDetailsQuery' 不是 在发现 /var/www/my-first-app/app/Http/Controllers/pagesController.php

我试过了;

  • 作曲家转储自动加载
  • 在 composer.json 文件中添加以下行

    “自动加载”:{ “psr-4”:{ “推进\”:“应用程序/模型/推进/” }}

但对我来说一切都很好..有什么建议吗??

【问题讨论】:

    标签: php laravel-5 propel


    【解决方案1】:
    "autoload": {
            "classmap": [
                "database",
                "app/models/propel"
            ]
    

    这解决了我的问题... :)

    【讨论】:

    • 您实际上并不需要在自动加载中添加这些行。我注意到您的命名空间不正确。 Laravel 5 足够聪明,可以在命名空间正确后加载您的类。
    【解决方案2】:

    我不断收到类似的缺失类错误。虽然我完全遵循You Must Use Composer laracast 中的 laracast 自动加载示例(包括对 composer.json 的修改),但我遇到了缺少类错误。

    在观看视频时,我注意到 jeffery 方式偶尔会在对象名称前使用 \ .....我不知道为什么,但我很沮丧地添加了它,它对我有用。

    总而言之,我发现我必须在对象前使用“\”,如下所示:

    新的\酒吧;

    我希望这会有所帮助,如果有人可以帮助我了解发生了什么,请告诉我们。

    【讨论】:

      猜你喜欢
      • 2018-01-17
      • 2016-02-08
      • 2016-04-09
      • 2018-07-20
      • 2013-06-30
      • 2016-08-08
      • 2013-08-01
      • 1970-01-01
      • 2012-05-16
      相关资源
      最近更新 更多