私はこのようなツリーがあります。symfonyの3サービスが見つからない例外
src
`-- AppBundle
|-- AppBundle.php
|-- Controller
| `-- MyController.php
`-- Service
`-- MyStringService.php
今、私はこのような「MyController」のサービス「MyStringService」を使用したい:
<?php
namespace AppBundle\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\Date;
use Symfony\Component\VarDumper\Cloner\Data;
class MyController extends Controller
{
public function usernameAction(Request $request, $username)
{
$data = $this->get('my_string_service')->getString($username);
return $this->render('profile.html.twig', $data);
}
}
は、それでは見てみましょうサービスでは、基本的には何もしません:
<?php
namespace AppBundle\Service;
class MyStringService
{
public function getString($string)
{
return $string;
}
}
私は私の次のもので私はそれを呼び出すことができますervices.yml:私はphp bin/console debug:container my_string_service
を使用する場合
services:
my_string_service:
class: AppBundle/Service/MyStringService
私が手:私はサービスを開始し、ページlocalhost:8000/
またはlocalhost:8000/MyUsername
を開いたときに
Information for Service "my_string_service"
===========================================
---------------- -----------------------------------
Option Value
---------------- -----------------------------------
Service ID my_string_service
Class AppBundle/Service/MyStringService
Tags -
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired yes
Autoconfigured yes
---------------- -----------------------------------
は今、私はServiceNotFoundException
を取得します。
今、私はsymfonyを使い始めましたが、私が紛失しているものは分かりません。アドバンス
なぜ 'public no'ですか? – Matteo