phpcomposer-phpautoloadclassnotfound

Class not found error with PHP Composer Autoload


The error is:

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

Project structure:

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

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

<?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 (located in src/Championsweb/Model/VO) looks like this:

<?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 looks like this:

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

index.php has the autoload require:

<?php

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

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',
);

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.

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.

Thanks in advance!

EDIT: This is what autoload_static.php looks like:

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

Solution

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

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

    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.