我有一个这样的树: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