簡體   English   中英

發現Symfony2文件類不在其中

[英]Symfony2 File Found Class Was Not In It

這是我的第一個問題,除了我不是英語母語人士,所以提前抱歉新手錯誤......

我從Symfony2開始,我已經面臨一個自動加載問題,我已經瘋了......

我只是想在我的AppBundle的DefaultController中使用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');
    }
}

在我的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');
    }
}

和我的config.yml片段:

services:
    amazon.crawler:
        class: Priceget\CollectorBundle\Crawler\Amazon

我已經嘗試過:

  • 空緩存
  • 重啟apache
  • 將類擴展到Controller? :-Z

非常感謝你提前。

你的命名空間是錯誤的,重命名它:

from: namespace Priceget\\CollectorBundle\\Crawler\\Amazon;

to: namespace Priceget\\CollectorBundle\\Crawler;

如果您不將<?php放在文件的開頭,也會發生此錯誤。

除了Igor所說的,如果你想讓它工作,你顯然必須在服務聲明(YML)中更改FQN類名。

這可能有點誤導,如果你沒有正確擴展你的課程,也會發生這種情況。 在我的實例中,我嘗試使用不正確的FQN擴展存儲庫:

class FilesRepository extends Doctrine\ORM\EntityRepository

本來應該:

class FilesRepository extends \Doctrine\ORM\EntityRepository

注意缺少反斜杠( \\ )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM