J'ai un arbre comme celui-ci:services Symfony 3 non trouvé exception
src
`-- AppBundle
|-- AppBundle.php
|-- Controller
| `-- MyController.php
`-- Service
`-- MyStringService.php
Maintenant, je veux utiliser le service "MyStringService" dans "MyController" comme ceci:
<?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);
}
}
Alors regardons au service, cela ne fondamentalement rien:
<?php
namespace AppBundle\Service;
class MyStringService
{
public function getString($string)
{
return $string;
}
}
Et pour que je puisse l'appeler par l'ID je suit dans mes s ervices.yml:
services:
my_string_service:
class: AppBundle/Service/MyStringService
Quand j'utilise php bin/console debug:container my_string_service
je reçois:
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
---------------- -----------------------------------
Maintenant, quand je commence le service et ouvrir la page localhost:8000/
ou localhost:8000/MyUsername
je reçois un ServiceNotFoundException
.
Alors maintenant je viens de commencer avec symfony et je ne sais pas ce qui me manque.
Merci à l'avance
pourquoi 'public no'? – Matteo