<p>You should try to keep your controllers as "thin" as possible. That is, they should call other classes, which do the actual work.</p>

<p>The Craft approach is to keep most of your code in service classes, and if you're just starting out with plugin development, I recommend you follow this pattern.</p>

<p>Bear in mind, though, that your service classes define your plugin's API.</p>

<p>In other words, if you add a public method to one of your plugin service classes, you're essentially telling everyone "hey, you can use this method if you like".</p>

<p>If you're interested, I wrote <a href="https://experiencehq.net/blog/the-hidden-perils-of-craft-plugin-service-classes" rel="nofollow noreferrer">a blog post</a> about this a while back.</p>

<p>In summary, keeping your code in services is much better than keeping it in controllers, but don't treat your service classes as a dumping ground for everything.</p>

CC BY-SA 3.0
2017-04-26T06:08:08.780Z
If that's the case, what is the difference between service and controller?
lance adrianne dela cruz
CC BY-SA 3.0
2017-04-26T06:13:49.063Z
Among many other things, controllers handle incoming HTTP requests. You should spend some time reading up on [Craft plugin development](https://craftcms.com/docs/plugins/introduction), the underlying [Yii framework](http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc), and [the MVC pattern](https://en.m.wikipedia.org/wiki/Model–view–controller) in general. StackExchange is not a substitute for independent study, and you need a solid understand of the basics.
Stephen Lewis
CC BY-SA 3.0
2017-04-26T06:18:49.987Z
No need to apologise, we're all learning. You're just going to struggle if you haven't taken the time to understand the basic concepts.
Stephen Lewis
CC BY-SA 3.0
2017-04-26T08:26:27.090Z
So the controller will be the one making the ajax call? correct me if I'm wrong sir :)
lance adrianne dela cruz
CC BY-SA 3.0
2017-04-26T14:21:42.100Z
No, the service makes the call to the external API. The controller handles requests _to_ your plugin.
Stephen Lewis
CC BY-SA 3.0
2017-04-27T04:59:33.720Z
Ok sir. Thanks. Is it applicable if i perform a jquery ajax to make an http request?
lance adrianne dela cruz
CC BY-SA 3.0
2017-04-27T05:22:04.610Z
HTTP requests to your plugin are handled by your controller, regardless of whether they use AJAX.
Stephen Lewis
CC BY-SA 3.0
2017-05-02T01:21:05.523Z
then what should i put int the services?
lance adrianne dela cruz
CC BY-SA 3.0
2017-05-02T01:23:46.213Z
We've covered this, at length, in the original answer, and the subsequent comments.
Stephen Lewis
CC BY-SA 3.0
2017-05-02T01:31:04.373Z
My bad. I didn't see. I thought I make a new post.
lance adrianne dela cruz
CC BY-SA 3.0
2017-04-26T06:02:21.433Z
2017-04-26T06:02:21.433Z
Stephen Lewis
<p>Where should I put my ajax call if I want to get some data from an external link or website? Should I put it in the controller, or in the service?</p>

<plugin-development>
Where should I make an HTTP REQUEST using AJAX?
# Where should I make an HTTP REQUEST using AJAX?

(Asked by: username_0 on 2017-04-26)

Where should I put my ajax call if I want to get some data from an external link or website? Should I put it in the controller, or in the service?

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2017-04-26. Score: 4 | Accepted answer

You should try to keep your controllers as "thin" as possible. That is, they should call other classes, which do the actual work.

The Craft approach is to keep most of your code in service classes, and if you're just starting out with plugin development, I recommend you follow this pattern.

Bear in mind, though, that your service classes define your plugin's API.

In other words, if you add a public method to one of your plugin service classes, you're essentially telling everyone "hey, you can use this method if you like".

If you're interested, I wrote [a blog post](https://experiencehq.net/blog/the-hidden-perils-of-craft-plugin-service-classes) about this a while back.

In summary, keeping your code in services is much better than keeping it in controllers, but don't treat your service classes as a dumping ground for everything.

> **Comments:**
> **username_0**: If that's the case, what is the difference between service and controller?  
> **username_1**: Among many other things, controllers handle incoming HTTP requests. You should spend some time reading up on [Craft plugin development](https://craftcms.com/docs/plugins/introduction), the underlying [Yii framework](http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc), and [the MVC pattern](https://en.m.wikipedia.org/wiki/Model–view–controller) in general. StackExchange is not a substitute for independent study, and you need a solid understand of the basics.  
> **username_1**: No need to apologise, we're all learning. You're just going to struggle if you haven't taken the time to understand the basic concepts.  
> **username_0**: So the controller will be the one making the ajax call? correct me if I'm wrong sir :)  
> **username_1**: No, the service makes the call to the external API. The controller handles requests _to_ your plugin.  
> **username_0**: Ok sir. Thanks. Is it applicable if i perform a jquery ajax to make an http request?  
> **username_1**: HTTP requests to your plugin are handled by your controller, regardless of whether they use AJAX.  
> **username_0**: then what should i put int the services?  
> **username_1**: We've covered this, at length, in the original answer, and the subsequent comments.  
> **username_0**: My bad. I didn't see. I thought I make a new post.  

---

