2016-09-29 88 views
1

服務負載我嘗試的Symfony 3(政黨成員例子)來創建一個簡單的服務的RuntimeException當symfony的

當我跑我的網頁,我得到這個錯誤:

自動加載預期類SocialBundle \ Services \ OCAntispam將在文件.. ../src/SocialBundle/Services/OCAntispam.php中定義爲 。找到 文件但該類不在其中,類名或 命名空間可能有拼寫錯誤。

我嚴格遵循教程(法文)由Sensio公司實驗室製造

我的服務代碼是

namespace SocialBundle\Antispam; 

class OCAntispam 
{ 
    public function isSpam($text) 
    { 
    return strlen($text) < 50; 
    } 
} 

在我的控制器

$antispam = $this->container->get('social.antispam'); 

而且services.yml

social.antispam: 
     class: SocialBundle\Services\OCAntispam 

如果它是一個錯字的錯誤,我瞎

感謝您的支持

編輯:

在我的服務,我想這樣的代碼:

namespace SocialBundle\Services\Antispam; 
namespace SocialBundle\Services\OCAntispam; 

沒有成功

+0

SocialBundle \ Services vs SocialBundle \ Antispam您是否可以發現差異? – Cerad

+0

我將命名空間SocialBundle \ Antispam更改爲命名空間SocialBundle \ Services \ Antispam和命名空間SocialBundle \ Services \ OCAntispam錯誤仍在此處 – user3279327

+0

更新您的問題並進行更改。你剛纔寫的沒有意義。我敢打賭,如果仔細觀察,會發現錯誤信息已更改。 – Cerad

回答

2

更改namespace服務類如下。

namespace SocialBundle\Services; 

class OCAntispam 
{ 
    public function isSpam($text) 
    { 
    return strlen($text) < 50; 
    } 
} 

Class定義的namespace不應該有Class名。

並確保您的文件OCAntispam.phpSocialBundle\Services之下。

這應該工作。

+0

就是這樣。謝謝 – user3279327