Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symfony 4 - Service "jms_serializer" not found #713

Closed
Jejec opened this issue Dec 31, 2018 · 8 comments
Closed

Symfony 4 - Service "jms_serializer" not found #713

Jejec opened this issue Dec 31, 2018 · 8 comments

Comments

@Jejec
Copy link

Jejec commented Dec 31, 2018

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.

@goetas
Copy link
Collaborator

goetas commented Dec 31, 2018

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.

@goetas goetas closed this as completed Dec 31, 2018
@Jejec
Copy link
Author

Jejec commented Dec 31, 2018

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.

@nreynis
Copy link

nreynis commented Jun 5, 2019

@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

@goetas
Copy link
Collaborator

goetas commented Jun 5, 2019

@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

@serrvius
Copy link

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

@softrare
Copy link

softrare commented Sep 5, 2019

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" :(

@michaljusiega
Copy link
Contributor

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

@softrare
Copy link

softrare commented Sep 6, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants