Services are just ordinary PHP classes, nothing special. But you must register it for the system to recognize. Here are the steps how you do it,
Create a regular PHP class (you can introduce other services if required)
namespace Acme\DemoBundle\Service; class MyService { private $session; public function _construct(SessionInterface $session ) { $this->session = $session; }
ok, we created a class, we need to register it in order to use it in the container service, here is how you do it:
The easiest way is to put it in the config.yml file, for example:
services: my_service: class: Acme\DemoBundle\Service\MyService arguments: - @session
or, otherwise, create a file (for example, services.yml , can be located in the config folder) and import it inside the config.yml file (the contents of the file coincide with the first method):
imports: - { resource: services.yml }
or you can create a services.yml file (the contents of the file matches the first method) inside the bundle Resources folder, specify it in accordance with the load method of your extension class (in accordance with DependencyInjection), (this requires a special directory and file structure, read about this in the document) :
class AcmeDemoExtension extends Extension { public function load(array $configs, ContainerBuilder $container) { $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources')); $loader->load('services.yml'); } }
In your case, you are not registering your service, the service container simply could not find it. Register it using one of the following methods.