PHP Composer自动加载导致类未找到错误

时间:2018-07-19 23:05:22

标签: php composer-php autoload classnotfound

错误是:

Fatal error: Uncaught Error: Class 'Championsweb\Model\VO\CompeticionVO'
not found in E:\Drive\Proyectos\ChampionsEclipse\public\CrearCompeticion.php
on line 4

项目结构:

├───config
├───public
│   ├───css
│   └───js
│       └───vendor
├───src
│   └───Championsweb
│       ├───Controller
│       └───Model
│           └───VO
├───templates
├───tests
├───vendor
│   └───composer
└───views

CrearCompeticion.php(位于public /中)如下所示:

<?php

if (isset($_POST) && sizeof($_POST) > 0) {
    $competicionVO = new \Championsweb\Model\VO\CompeticionVO(
        $_POST['nombre'],
        $_POST['anho']
    );
    $adminactions = new \Championsweb\Controller\AdminActions();
    $adminactions->crearCompeticion($competicionVO);
}

require '../views/CrearCompeticion.view.php';

CompeticionVO.php(位于src / Championsweb / Model / VO中)如下:

<?php
namespace Championsweb\Model\VO;

class CompeticionVO {
    public $id;
    public $nombre;
    public $anho;
    public $idGanador;

    public function __construct($nombre, $anho) {
        $this->nombre = $nombre;
        $this->anho = $anho;
    }
}

Composer.json看起来像这样:

{
    "autoload" : {
        "classmap" : [
            "./"
        ]
    }
}

index.php具有自动加载要求:

<?php

require '../vendor/autoload.php';

autoload_classmap.php具有CompeticionVO类:

<?php

// autoload_classmap.php @generated by Composer

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

return array(
    'Championsweb\\Controller\\Actions' => $baseDir . '/src/Championsweb/Controller/Actions.php',
    'Championsweb\\Controller\\AdminActions' => $baseDir . '/src/Championsweb/Controller/AdminActions.php',
    'Championsweb\\Controller\\UserActions' => $baseDir . '/src/Championsweb/Controller/UserActions.php',
    'Championsweb\\Model\\Db' => $baseDir . '/src/Championsweb/Model/Db.php',
    'Championsweb\\Model\\VO\\CompeticionVO' => $baseDir . '/src/Championsweb/Model/VO/CompeticionVO.php',
    'Championsweb\\Model\\VO\\EquipoVO' => $baseDir . '/src/Championsweb/Model/VO/EquipoVO.php',
    'Championsweb\\Model\\VO\\RondaVO' => $baseDir . '/src/Championsweb/Model/VO/RondaVO.php',
    'Championsweb\\Model\\VO\\UsuarioVO' => $baseDir . '/src/Championsweb/Model/VO/UsuarioVO.php',
    'ComposerAutoloaderInit91342042e1463ce66f1dcacb1f34d909' => $vendorDir . '/composer/autoload_real.php',
    'Composer\\Autoload\\ClassLoader' => $vendorDir . '/composer/ClassLoader.php',
    'Composer\\Autoload\\ComposerStaticInit91342042e1463ce66f1dcacb1f34d909' => $vendorDir . '/composer/autoload_static.php',
);

基本上,CrearCompeticion.view.php具有通过POST传递到CrearCompeticion.php的形式。然后CrearCompeticion.php尝试使用表单的信息创建CompeticionVO的实例,但出现错误。

我在做什么错?另外,我从一些教程中获得了composer.json文件,但我不太了解它的工作原理,我很乐意。

谢谢!

编辑:这就是autoload_static.php的样子:

<?php

// autoload_static.php @generated by Composer

namespace Composer\Autoload;

class ComposerStaticInit91342042e1463ce66f1dcacb1f34d909
{
    public static $classMap = array (
        'Championsweb\\Controller\\Actions' => __DIR__ . '/../..' . '/src/Championsweb/Controller/Actions.php',
        'Championsweb\\Controller\\AdminActions' => __DIR__ . '/../..' . '/src/Championsweb/Controller/AdminActions.php',
        'Championsweb\\Controller\\UserActions' => __DIR__ . '/../..' . '/src/Championsweb/Controller/UserActions.php',
        'Championsweb\\Model\\Db' => __DIR__ . '/../..' . '/src/Championsweb/Model/Db.php',
        'Championsweb\\Model\\VO\\CompeticionVO' => __DIR__ . '/../..' . '/src/Championsweb/Model/VO/CompeticionVO.php',
        'Championsweb\\Model\\VO\\EquipoVO' => __DIR__ . '/../..' . '/src/Championsweb/Model/VO/EquipoVO.php',
        'Championsweb\\Model\\VO\\RondaVO' => __DIR__ . '/../..' . '/src/Championsweb/Model/VO/RondaVO.php',
        'Championsweb\\Model\\VO\\UsuarioVO' => __DIR__ . '/../..' . '/src/Championsweb/Model/VO/UsuarioVO.php',
        'ComposerAutoloaderInit91342042e1463ce66f1dcacb1f34d909' => __DIR__ . '/..' . '/composer/autoload_real.php',
        'Composer\\Autoload\\ClassLoader' => __DIR__ . '/..' . '/composer/ClassLoader.php',
        'Composer\\Autoload\\ComposerStaticInit91342042e1463ce66f1dcacb1f34d909' => __DIR__ . '/..' . '/composer/autoload_static.php',
    );

    public static function getInitializer(ClassLoader $loader)
    {
        return \Closure::bind(function () use ($loader) {
            $loader->classMap = ComposerStaticInit91342042e1463ce66f1dcacb1f34d909::$classMap;

        }, null, ClassLoader::class);
    }
}

1 个答案:

答案 0 :(得分:1)

正如您所说,您正在发布到位于 <ng-container *ngFor="let branch of branches"> <tr > <td (click)="onExpandClick(branch)" contenteditable="true">{{branch.name}}</td> <td contenteditable="true">Rice A</td> <td><label class="switch"> <input type="checkbox" id="hide-show-keyfacts" checked> <span class="slider round onoff"></span> </label></td> </tr> <tr *ngFor="let c of branch.bi" ngClass="" > <td (click)="onExpandClick(branch)" contenteditable="true">{{c.name}}</td> <td contenteditable="true">Rice A</td> <td><label class="switch"> <input type="checkbox" id="hide-show-keyfacts" checked> <span class="slider round onoff"></span> </label></td> </tr> </ng-container> 目录中的名为CrearCompeticion.php的脚本。

这意味着在这种情况下,不会执行public/中存在的任何代码,包括index.php

因此,在您的情况下(您说您遵循了Laracast,但似乎没有使用Laravel应用设置),那么您也想在require '../vendor/autoload.php';的顶部添加require __DIR__ . '/../vendor/autoload.php';,应该做的事。