3
votes

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);
    }
}
1
The thing with composer is... either you have to fully install the crap and use all of its myriad of files and setup directly from it... or it just doesn't work. Or you roll your own proper autoloader for the files in the library or framework you are wanting to use and then never worry about it. But piecing bits from each method, tends to lead to issues like this. - IncredibleHat
@iporto Did you run composer update on command line or did you copy stuff from your tutorial? The command should update autoload_static.php (I doubt autoload_classmap.php is used). Please post its contents if it still doesn't work. - Jeto
@Jeto I just copied the contents of composer.json. The rest of the files were generated using composer install. I added the contents of autoload_classmap.php to the post. Thank you! - iporto
@IncredibleHat I don't get what you mean. The tutorial I followed is this one: laracasts.com/series/php-for-beginners/episodes/21. In there, it says that this composer.json I'm using serves to autoload all classes. I'm willing to change my composer.json or anything to make this work, I just don't know how to do it. Thank you! - iporto

1 Answers

4
votes

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.