试图加载class" ClassName"来自命名空间(...)。即使导入名称空间

时间:2015-01-30 00:55:34

标签: php symfony namespaces

我有一个Symfony项目,我添加了一些包含各种类的非symfony php文件。但由于某些原因,在加载网站时未加载类,即使IDE正确看到它们。

所以,我有一个需要其他类的类:

namespace rootspace\FrontBundle\Controller;

use rootspace\FrontBundle\Networks\TwitterOAuth;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class TwitterController extends Controller
{
    public function connectAction(){
        // The TwitterOAuth instance
        $connection = new TwitterOAuth('abc', '123');
    }
}

然后是无法加载的类(需要另一个文件)

namespace rootspace\FrontBundle\Networks;

/* Load OAuth lib. You can find it at http://oauth.net */
//require_once('OAuth.php'); -- should this be commented out?

/**
 * Twitter OAuth class
 */
class TwitterOAuth {
  /* Contains the last HTTP status code returned. */
}

最后,第三个文件

namespace rootspace\FrontBundle\Networks;
use Symfony\Component\Config\Definition\Exception\Exception;

class OAuthConsumer
{
    public $key;
    public $secret;
}
(...)

我认为实际的文件名无所谓,对吧?他们的结构呢? PhpStorm可以正确查看所有类,我可以右键单击它们,但在部署时会失败。

感谢您的帮助

编辑 - 整个错误消息显示

Attempted to load class "TwitterOAuth" from namespace "rootspace\FrontBundle\Networks" in D:\Dropbox\project\src\rootspace\FrontBundle\Controller\TwitterController.php line 15. Do you need to "use" it from another namespace?

1 个答案:

答案 0 :(得分:3)

这是因为Symfony的自动加载器遵循PSR标准(PSR-0PSR-4),它表示完全限定(带名称空间)类名称转换为文件位置和名称。事实上,文件名确实很重要。 因此,在您的情况下,rootspace\FrontBundle\Networks\TwitterOAuth类应位于名为rootspace/FrontBundle/Networks的文件的TwitterOAuth.php目录中

如果您使用的课​​程不符合PSR标准,您也可以在app/autoloader.php档案中手动注册

检查这些以获取更多信息:

How can I add a namespace to Symfony 2.1?

How to autoload class

并查看this回答