1

Although the question appears subjective, I suspect there is a right way and wrong way to do this. I will explain.

The practice at my company has been to haphazardly mingle cURL calls to external APIs into a Magento Module's model folder.

A better way of handling this is to abstract API connection to an external class. It is the external classes job to "know" how to connect to the actual service, when to use a different URL for testing and so on.

My question is where in a Magento module should such a class live? It's clearly not part of the data model, nor is it a Block or Controller.

  1. Should I create an "Api" folder and put whatever classes I need in there, named "Company_Module_Api_Zendesk"?

  2. If yes, and I do that, will the autoloader be able to handle it? Can I instantiate such a class by using "new Company_Module_Api_Zendesk()" or do I have to require() it myself?

If this question must be closed due to excessive subjectivity, could someone point me towards a relevant guide or tutorial?

1

I don't see anything wrong with placing an API class under the extension's Model folder. That would allow you to use Mage::getModel() (and potentially 3rd party extensions that would like to rewrite your class).

One other approach, if you can decouple your API class completely from Magentoisms, is to place them under the /lib folder.

|improve this answer|||||
  • It seems to me like people overuse the model in Magento. While an API does count as data access, it's not exactly a datastore like a DB and seems like the wrong layer to have the responsibility of talking with a third party service. Also, as noted in my edit, PHP's autoloading works if you put the class someplace else, there isn't any benefit to using the model's factory that I can see. – glimmertea Mar 10 '15 at 17:06
1

I use a rule of thumb: if the class dies not need Magento to perform its job, I stick it in lib. Otherwise in app. This seems to be consistent with Magento, look for example at lib/Varien/Http. The adapters in their are driven by core code, but they don't know what Magento is themselves.

|improve this answer|||||
0

Answering my own question again.

I don't know if these are necessarily the best practices. I'm still new to coding within the Magento framework.

I elected to put my API connection class in an "Api" folder as I described. The answer to 2) is yes, the autoloader does automatically pick it up when I do this.

I had thought I would be using class groups, XML and perhaps a factory method in order to access my classes but it turned out to be quite simple.

|improve this answer|||||
0

I've used APIs in helper folders and in model folders. The only limitation I came across is having cron being able to access them. Cron is tied to models so that's where my api folders go.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.