php Symfony2 文件发现类不在其中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19080796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony2 File Found Class Was Not In It
提问by manelescuer
This is my first question, besides I'm not english-native speaker, so sorry in advance for newbie mistakes...
这是我的第一个问题,此外我不是英语母语人士,所以对于新手错误提前抱歉......
I'm starting with Symfony2, and I've been facing an autoload problem for a couple of days, i'm getting crazy..
我从 Symfony2 开始,几天来我一直面临自动加载问题,我快疯了..
I'm just trying to use a PHP class inside my DefaultController of my AppBundle. I've read the way of doing this is by creating a service in my config.yml and giving a namespace to that class that matches.
我只是想在我的 AppBundle 的 DefaultController 中使用 PHP 类。我读过这样做的方法是在我的 config.yml 中创建一个服务并为匹配的类提供命名空间。
Symfony tells me that it does found the file but the class is not in it, the exact error is:
Symfony 告诉我它确实找到了该文件,但该类不在其中,确切的错误是:
The autoloader expected class "Priceget\CollectorBundle\Crawler\Amazon" to be defined in file "/srv/www/lol.com/public_html/priceget/symfony/src/Priceget/CollectorBundle/Crawler/Amazon.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
自动加载器期望在文件“/srv/www/lol.com/public_html/priceget/symfony/src/Priceget/CollectorBundle/Crawler/Amazon.php”中定义类“Priceget\CollectorBundle\Crawler\Amazon”。找到了文件但类不在其中,类名或命名空间可能有拼写错误。
And my class is just this:
我的课就是这样的:
<?php
namespace Priceget\CollectorBundle\Crawler\Amazon;
use Symfony\Component\HttpFoundation\Response;
class Amazon
{
public function getAll()
{
return new Response('l0l');
}
}
In my DefaultController I'm calling it like that:
在我的 DefaultController 中,我这样称呼它:
<?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');
}
}
And my config.yml piece:
还有我的 config.yml 片段:
services:
amazon.crawler:
class: Priceget\CollectorBundle\Crawler\Amazon
I've already tried to:
我已经尝试过:
- Empty cache
- Restart apache
- Extend the class to Controller? :-Z
- 清空缓存
- 重启apache
- 将类扩展到控制器?:-Z
Thank you so much in advance.
非常感谢你。
回答by Igor Pantovi?
Your namespace is wrong, rename it:
你的命名空间错了,重命名:
from: namespace Priceget\CollectorBundle\Crawler\Amazon;
从: namespace Priceget\CollectorBundle\Crawler\Amazon;
to: namespace Priceget\CollectorBundle\Crawler;
到: namespace Priceget\CollectorBundle\Crawler;
回答by Anton Andreev
This error also occurs if you do not put <?php
in the beginning of the file.
如果您没有放入<?php
文件的开头,也会发生此错误。
回答by Edward
This can be a bit misleading, it also happens if you don't extend your class correctly. In my instance I tried to extend a repository with an incorrect FQN:
这可能有点误导,如果你没有正确扩展你的类也会发生这种情况。在我的实例中,我尝试使用不正确的 FQN 扩展存储库:
class FilesRepository extends Doctrine\ORM\EntityRepository
should have been:
本来应该:
class FilesRepository extends \Doctrine\ORM\EntityRepository
Notice the missing backslash (\
).
请注意缺少的反斜杠 ( \
)。
回答by Christian Soronellas Vallespí
In addition to what's said by Igor, you obviously have to change the FQN class name in the service declaration (YML) if you want it to work.
除了 Igor 所说的之外,如果您希望它工作,您显然必须更改服务声明 (YML) 中的 FQN 类名称。