"Most of the logic of your site and of your module belongs in services."

- Larry Garfield @ DrupalCon Los Angeles 2015: Drupal 8: The Crash Course - https://youtu.be/8vwC_01KFLo?t=2387

What means "most". I successfully created a service class. And it is injected into my controller all fine and working nicely. But I am not sure that I have a problem that needs a service.

What I want to do:

I need to load a custom XML file from an external source dependent on an ID in the URL and get out some content. Later I want to store some of the XML content into a node or a custom entity.

  1. So I load the XML with guzzle in my controller. -> Works great
  2. I put the XML into an DOMCrawler object. -> Nice
  3. I use the crawler to get some data out of the XML -> OK
  4. As I am not supposed to have any logic in my controller I created a service class that is named CustomXmlParser - it stores the Crawler Object and I added get methods to get out specific data from the XML. -> Alright

But having a CustomXmlParser service makes somehow no sense to me. I get the service object in the controller. Than I load the XML in the controller dependent on the URL arguments. Now I can handover the XML to my parser service. But this makes not really sense. Isn't a service supposed to be useable right after getting the instance?

Is this really a case for a service? Or can I just use some class I put in my module directory?

Or maybe my "service" should do more. Would it be the right thing to have the parser initialized with the http request so it can load the external XML file itself? But than I can't give the parser a fake xml string during a test. So this would be wrong I guess.

Maybe someone can point me in the right direction how this is done right in Drupal 8.
Thanks.

Comments

dotmundo’s picture

Are you talking about following MVC guidelines?