Best place to store API methods

Published 1 year ago by lorvent

I want to make many API calls and it comes to around 50 methods, whom i am going to use in many Controllers,

so i am wondering what is the best place to store all those methods?

thanks.

Best Answer (As Selected By lorvent)
d3xt3r

What i meant by situations is that, code refactoring depends on the code/methods we are dealing with.

  1. If your methods are instance methods for controller, you can extract them in base controller or try exploring traits.
  2. If these are static/helper methods you can even place in their own classes or helper files.

There isn't one size fits all solution for this. :)

SaeedPrez
SaeedPrez
1 year ago (308,410 XP)

I usually create a Library folder inside my project's app folder, in there I put all my helper classes. You could even categorize them further, for example App/Library/Traits/Doable.php

lorvent
lorvent
1 year ago (13,385 XP)

Thanks Saeed, yes Traits looks like good location, but i am confused with too many options :( like traits, repositories

SaeedPrez
SaeedPrez
1 year ago (308,410 XP)
- app
    - Http
    - Jobs
    - Library
        - Traits
        - Repositories
        - Foo
        - Bar
    - Listeners
    - Policies

Laravel is very flexible on this, there is really no right or wrong. Do what makes sense for you, what is important is that 6 months from now, you will know where to look for them.

d3xt3r
d3xt3r
1 year ago (146,330 XP)

@Lorven You should not be ... each one of carry their own meaning. If its about methods that you only use in api controllers, perhaps you can extract them to a base controller and have all other controllers extend them.

lorvent
lorvent
1 year ago (13,385 XP)

@d3xt3r Currently i am doing that approach only but want to know, is there any better option.

d3xt3r
d3xt3r
1 year ago (146,330 XP)

@lorvent Better options depend on situations ... I am not sure here what requirements are ... can you elaborate ...

lorvent
lorvent
1 year ago (13,385 XP)

I am making a connect to an API, there are 50+ methods.

i make around 10 methods from each controller.

currently, i wrote a wrapper method to make calls and put it in my base controller, so i am making calls with $this->method()

d3xt3r
d3xt3r
1 year ago (146,330 XP)

What i meant by situations is that, code refactoring depends on the code/methods we are dealing with.

  1. If your methods are instance methods for controller, you can extract them in base controller or try exploring traits.
  2. If these are static/helper methods you can even place in their own classes or helper files.

There isn't one size fits all solution for this. :)

Please sign in or create an account to participate in this conversation.