Answer the question
In order to leave comments, you need to log in
How to properly create a service in Symfony 3.4?
Good day! Please tell me how to create a service in symfony 3.4 correctly. Everything seems to be done according to the manuals, but it still crashes Uncaught PHP ExceptionSymfony\Component\DependencyInjection\Exception\ServiceNotFoundException: "You have requested a non-existent service "obb.naming"
Controller:
namespace Ost\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class TestController extends Controller
{
public function heyAction()
{
$name= 'qqqq';
$naming = $this->get("obb.naming");
$naming->setName($name);
return $this->render("Test/hey.html.twig",array('message'=>$naming->getName()));
}
}
namespace Ost\BlogBundle\Services;
class Naming
{
private $name;
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
obb.naming:
class: Ost\BlogBundle\Services\Naming
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: "@OstBlogBundle/Resources/config/services.yml" }
Answer the question
In order to leave comments, you need to log in
In version 3.4, it is customary to name services by the class name, and if the service does not have any specific dependencies, then it does not need to be registered in the services.yml file. In an action, it is better not to use $this->get('...'), but to explicitly pass the required service:
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Ost\BlogBundle\Services\Naming;
class TestController extends Controller
{
public function key(Naming $naming)
{
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question