Class 未发现 PHP Composer Autoload 错误
Class not found error with PHP Composer Autoload
错误是:
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 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 实例,但我得到了错误。
我做错了什么?另外,我从一些教程中获得了 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);
}
}
正如您所说,您要发布到位于 public/
目录中的名为 CrearCompeticion.php
的脚本。
这意味着 index.php
中出现的任何代码,包括 require '../vendor/autoload.php';
,在这种情况下都不会执行。
所以在你的情况下(你说你关注了 Laracast 但似乎没有使用 Laravel 应用程序设置),你需要在 [= 之上添加 require __DIR__ . '/../vendor/autoload.php';
10=] 也应该可以完成工作。
错误是:
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 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 实例,但我得到了错误。
我做错了什么?另外,我从一些教程中获得了 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);
}
}
正如您所说,您要发布到位于 public/
目录中的名为 CrearCompeticion.php
的脚本。
这意味着 index.php
中出现的任何代码,包括 require '../vendor/autoload.php';
,在这种情况下都不会执行。
所以在你的情况下(你说你关注了 Laracast 但似乎没有使用 Laravel 应用程序设置),你需要在 [= 之上添加 require __DIR__ . '/../vendor/autoload.php';
10=] 也应该可以完成工作。