我開始與Symfony的服務工作,並因此創造了從symfony的文檔,示例服務:Symfony的現有公共服務引發的錯誤消息「您請求一個不存在的服務」
namespace AppBundle\Service;
use Psr\Log\LoggerInterface;
class MessageGenerator
{
private $logger;
public function __construct(LoggerInterface $logger){
}
public function getMessage()
{
$this->logger->info('Success!');
}
}
我稱之爲在我的控制器服務(我也有使用說明: :use AppBundle\Service\MessageGenerator;
$messageGenerator = $this->get(MessageGenerator::class);
$message = $messageGenerator->getMessage();
$this->addFlash('success', $message);
我的服務就是在services.yml文件中定義:
app.message_generator:
class: AppBundle\Service\MessageGenerator
public: true
所以在我的眼裏,我做了完全一樣的文檔中描述的一切,打電話時:
php app/console debug:container app.message_generator
在我的命令行
我讓我的服務:
Option Value
------------------ ------------------------------------
Service ID app.message_generator
Class AppBundle\Service\MessageGenerator
Tags -
Scope container
Public yes
Synthetic no
Lazy no
Synchronized no
Abstract no
Autowired no
Autowiring Types -
現在,當我執行控制器功能,我打電話給我的服務我仍然得到錯誤:
You have requested a non-existent service "appbundle\service\messagegenerator".
任何ide如?
時,或作爲替代方案,在服務定義文件中的類名稱替換app.message_generator。這反過來可以讓你消除類:線,也更符合最新的集裝箱推薦做法。 – Cerad
謝謝@sics,解決了我的問題! – sonja