Jmsserializerbundle: Symfony 4 - Service "jms_serializer" not found

Created on 31 Dec 2018  路  8Comments  路  Source: schmittjoh/JMSSerializerBundle

Hello,

I discovered Symfony 4 and I wanted to use the jms_serializer service. So I used the following command:
php composer require jms / serializer-bundle

And in my code I use:
$ this-> get ('jms_serializer') -> serialize ($ object, 'json');

But I have the following error:
Service "jms_serializer" not found: even though it exists in the app's container, the container inside "App\Controller\ArticleController" is a smaller service locator that only knows about the "doctrine", "http_kernel", "parameter_bag", "request_stack", "router" and "session" services.Try using dependency injection instead.

Yet they do not say in the documentation that I have to use addiction injection.

What do I have to do?
If I have to use dependency injection can you give me a code example that I understand how to use it?

A big thank you in advance.

Most helpful comment

With symfony 4, you should inject the serializer service into your controller somehow.

see https://symfony.com/doc/current/service_container/injection_types.html#constructor-injection

One way is to declare it in the constructor.

one the ways is to use public static function getSubscribedServices() inside of needed controller it needs to look somehow like that

public static function getSubscribedServices() { return array_merge(parent::getSubscribedServices(), [ 'jms_serializer' => '?'.SerializerInterface::class, ]); }

after it will be available in the container with the following name that are you define it
$this->container->get('jms_serializer')

the information about that you can find here https://symfony.com/doc/current/service_container/service_subscribers_locators.html

All 8 comments

With symfony 4, you should inject the serializer service into your controller somehow.

see https://symfony.com/doc/current/service_container/injection_types.html#constructor-injection

One way is to declare it in the constructor.

Okay I think I understand but how do I call jms_serializer?

like this:
public function _constructor (JMSSerialize $jsmSerialize)

EDIT: I found. We must do SerializerInterface $serializer.
A big thank you for your help.

@goetas, even though there is no issue with the code, there is one with the documentation.
The first line of code the user see is deprecated. It would really help if this got updated:
http://jmsyst.com/bundles/JMSSerializerBundle#usage

@nreynis thanks for pointing out this.
Can you please send a pr for the documentation? A good starting point is https://github.com/schmittjoh/JMSSerializerBundle/tree/master/Resources/doc

With symfony 4, you should inject the serializer service into your controller somehow.

see https://symfony.com/doc/current/service_container/injection_types.html#constructor-injection

One way is to declare it in the constructor.

one the ways is to use public static function getSubscribedServices() inside of needed controller it needs to look somehow like that

public static function getSubscribedServices() { return array_merge(parent::getSubscribedServices(), [ 'jms_serializer' => '?'.SerializerInterface::class, ]); }

after it will be available in the container with the following name that are you define it
$this->container->get('jms_serializer')

the information about that you can find here https://symfony.com/doc/current/service_container/service_subscribers_locators.html

after it will be available in the container with the following name that are you define it
$this->container->get('jms_serializer')

gives me "Call to a member function get() on null" :(

You must set container content before calling get(). Pass the ContainerInterface to $this->setContainer() in AbstractController.

Thank you. I got the container now, but I always get the message that the service "jms_interface" or "JMS\Serializer\SerializerInterface" (through dependency injection) is not running. So I think I am missing something. How can I make sure this service is started/running? I'm new at Symfony.

Edit:
I reinstalled the package, then it worked. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thaonx picture thaonx  路  5Comments

simshaun picture simshaun  路  9Comments

tcardonne picture tcardonne  路  3Comments

grimabe picture grimabe  路  6Comments

yyaremenko picture yyaremenko  路  4Comments