Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JhumanJ's avatar

Creating a package for an API

Hello!

I found this train API online that I want to use in my Laravel project. I was wondering what is the best way to integrate it? Doing some curl call straight in my controllers definitely does not seem to be a great idea. I'm never done such a thing before, should I create a package? If yes any good ressource about that?

Thank you!

0 likes
4 replies
zachleigh's avatar
Level 47

For a start, I would wrap the api calls up in a class, have the class implement an interface, and then code to this interface in your project. This way, all the api calls will be in a single place and if you swap out the api later, you can simply make a new implementation of the interface.

JhumanJ's avatar

Thanks for your reply @zachleigh! That's what I was pretty much thinking but I have some doubt regarding where I should save this class?

zachleigh's avatar

Thats up to you :) I would probably just make a directory for it, so maybe app/TrainApi or something like that. You can then throw the interface and your class in there. Or you could make it into a package and then just install it through composer.

1 like
JhumanJ's avatar

Alright, I'm gonna go for the app version (never done any package :) )! Thanks for your help!

Please or to participate in this conversation.