这是我的第一个问题,此外我不是英语母语人士,所以提前对新手的错误表示歉意......
我从Symfony2开始,几天来我一直面临自动加载问题,我变得疯狂了。
我只是尝试在我的AppBundle的默认控制器中使用PHP类。我已经阅读了执行此操作的方法,方法是在我的config.yml中创建一个服务,并为匹配的类提供一个命名空间。
Symfony告诉我它确实找到了文件,但类不在其中,确切的错误是:
自动加载器期望类"Priceget''CollectorBundle''Crawler''Amazon"在文件"/srv/www/lol.com/public_html/priceget/symfony/src/Priceget/CollectorBundle/Crawler/Amazon.php"中定义。找到文件,但类不在其中,类名或命名空间可能有拼写错误。
我的课就是这样的:
<?php
namespace Priceget'CollectorBundle'Crawler'Amazon;
use Symfony'Component'HttpFoundation'Response;
class Amazon
{
public function getAll()
{
return new Response('l0l');
}
}
在我的默认控制器中,我是这样称呼它的:
<?php
namespace Priceget'CollectorBundle'Controller;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
use Guzzle'Http'Client;
use Symfony'Component'DomCrawler'Crawler;
use Priceget'CollectorBundle'Crawler'Amazon;
class DefaultController extends Controller
{
public function indexAction()
{
$amazon = $this->get('amazon.crawler');
}
}
还有我的 config.yml 文章:
services:
amazon.crawler:
class: Priceget'CollectorBundle'Crawler'Amazon
我已经尝试过:
- 空缓存
- 重启阿帕奇
- 将类扩展到控制器? :-Z
提前非常感谢你。
您的命名空间错误,请重命名它:
从: namespace Priceget'CollectorBundle'Crawler'Amazon;
至: namespace Priceget'CollectorBundle'Crawler;
如果未将<?php
放在文件的开头,也会发生此错误。
除了 Igor(伊戈尔(所说的之外,如果您希望它正常工作,显然必须更改服务声明 (YML( 中的 FQN 类名。
这可能有点误导,如果您没有正确扩展类,也会发生这种情况。在我的实例中,我尝试使用不正确的 FQN 扩展存储库:
class FilesRepository extends Doctrine'ORM'EntityRepository
应该是:
class FilesRepository extends 'Doctrine'ORM'EntityRepository
请注意缺少的反斜杠 ( '
(。