我已经成功安装并设置了Symfony 2,并一直遵循文档。
I'm current up to http://symfony.com/doc/2.0/book/doctrine.html
一切都很好,直到我到达这一行:
php app/console doctrine:generate:entities Acme/StoreBundle/Entity/Product
,此时我得到以下错误:
[RuntimeException]
The autoloader expected class "AcmeStoreBundleEntityProduct" to be defined
in file "C:Program Files (x86)Apache Software FoundationApache2.2htdocsSymf
onyapp/../srcAcmeStoreBundleEntityProduct.php". The file was found but the
class was not in it, the class name or namespace probably has a typo.
我在Linux和Windows机器上都遇到过这种情况。
Product.php的内容和教程一样:
// src/Acme/StoreBundle/Entity/Product.php
namespace AcmeStoreBundleEntity;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
* @ORMTable(name="product")
*/
class Product
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="string", length=100)
*/
protected $name;
/**
* @ORMColumn(type="decimal", scale=2)
*/
protected $price;
/**
* @ORMColumn(type="text")
*/
protected $description;
}
该消息来自DebugUniversalClassLoader.php:
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
require $file;
if (!class_exists($class, false) && !interface_exists($class, false)) {
throw new RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file));
}
}
}
因此,文件必须在那里,并且可读,否则findFile
和require
将无法工作。所有这些检查所做的是require()
文件,然后使用标准的PHP class_exists()来查看类现在是否在那里。
我能想到的唯一的事情可能会导致这个消息给定这些文件内容:你已经错过了<?php
在文件的开始。现在,我知道这有点冒险,但老实说,我想不出任何其他不会导致其他类型错误的方法。
我有同样的错误"文件被找到,但类不在其中,类名或命名空间可能有拼写错误。"只是因为
<?php
文件开头缺少!!啊,从教程中复制粘贴…再见!