我有一個這樣的樹: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;
}
}
而且,這樣我可以通過ID調用它我在小號以下ervices.yml:
services:
my_string_service:
class: AppBundle/Service/MyStringService
當我使用php bin/console debug:container my_string_service
我得到:
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
---------------- -----------------------------------
現在,當我啓動服務,並打開網頁localhost:8000/
或localhost:8000/MyUsername
我得到一個ServiceNotFoundException
。
所以現在我只是從symfony開始,不知道我在想什麼。
由於提前
爲什麼'公開不'? – Matteo