PHP Composer Autoload 找不到类错误 [英] Class not found error with PHP Composer Autoload

查看:33
本文介绍了PHP Composer Autoload 找不到类错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误是:

Fatal error: Uncaught Error: Class 'ChampionswebModelVOCompeticionVO'
not found in E:DriveProyectosChampionsEclipsepublicCrearCompeticion.php
on line 4

项目结构:

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

CrearCompeticion.php(位于 public/)看起来像这样:

CrearCompeticion.php (located in public/) looks like this:

<?php

if (isset($_POST) && sizeof($_POST) > 0) {
    $competicionVO = new ChampionswebModelVOCompeticionVO(
        $_POST['nombre'],
        $_POST['anho']
    );
    $adminactions = new ChampionswebControllerAdminActions();
    $adminactions->crearCompeticion($competicionVO);
}

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

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

CompeticionVO.php (located in src/Championsweb/Model/VO) looks like this:

<?php
namespace ChampionswebModelVO;

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

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

Composer.json 看起来像这样:

Composer.json looks like this:

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

index.php 有自动加载要求:

index.php has the autoload require:

<?php

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

autoload_classmap.php 有 CompeticionVO 类:

autoload_classmap.php has the CompeticionVO class:

<?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 的实例,但出现错误.

Basically, CrearCompeticion.view.php has a form that is passed through POST to CrearCompeticion.php. Then CrearCompeticion.php tries to create an instance of CompeticionVO with the info of the form, but I get the error.

我做错了什么?另外,我从一些教程中获得了我的 composer.json 文件,但我不太了解它是如何工作的,我很想这样做.

What am I doing wrong? Also, I got my composer.json file from some tutorial, but I don't really understand how it works and I'd love to.

提前致谢!

这是 autoload_static.php 的样子:

This is what autoload_static.php looks like:

<?php

// autoload_static.php @generated by Composer

namespace ComposerAutoload;

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);
    }
}

推荐答案

如您所说,您正在发布一个名为 CrearCompeticion.php 的脚本,该脚本位于 public/ 目录.

So as you said, you're posting to a script called CrearCompeticion.php that is located within the public/ directory.

这意味着 index.php 中存在的任何代码,包括 require '../vendor/autoload.php';,在这种情况下都不会执行.

This means that whatever code is present in index.php, including require '../vendor/autoload.php';, is not executed in this case.

所以在你的情况下(你说你关注了 Laracast 但似乎没有使用 Laravel 应用程序设置),你需要添加 require __DIR__ .'/../vendor/autoload.php'; 位于 CrearCompeticion.php 之上,这应该可以完成这项工作.

So in your case (you said you followed a Laracast but don't seem to be using a Laravel app setup), you'll want to add require __DIR__ . '/../vendor/autoload.php'; on top of CrearCompeticion.php as well, which should do the job.

这篇关于PHP Composer Autoload 找不到类错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆