<p>Putting aside the term "CRUD" for a moment, it essentially breaks down like this...</p>

<ul>
<li>If something wants to ping your site, it is (almost) inevitably going to be pinging a Controller.</li>
<li>If you want to do something in the database (create, update, delete), you should probably be doing that in a Service.</li>
</ul>

<p>Which raises the question, <em>"how/where to bridge that gap?"</em></p>

<p>The answer is, <em>"it depends."</em></p>

<p>You could keep most of your logic in the Controller, and just move the DB interactions to the Service. Or you could immediately ping a Service method as soon as your Controller is hit. Either way is fine, and it depends heavily on what the site requires, and what you (as the developer) are more comfortable with.</p>

<p>As you noted, sometimes people bend the rules and put DB interactions directly into the Controller method. While that's frowned upon, there may not be any negative repercussions. So don't worry too much about how other people are handling it... just do what makes sense to you.</p>

CC BY-SA 4.0
2018-08-29T16:54:23.050Z
2018-08-29T16:54:23.050Z
Lindsey D
<p>The documentation states:</p>

<blockquote>
  <p>Many service methods perform some sort of operation for a given model, such as a CRUD operation.</p>
</blockquote>

<p><a href="https://docs.craftcms.com/v3/extend/services.html#model-operation-methods" rel="nofollow noreferrer">(Link)</a></p>

<p>But browsing through existing plugins, I see the CRUD logic is most often done in controllers. </p>

<p>What's the best practice here?</p>

CC BY-SA 4.0
2018-08-29T15:49:00.817Z
Sorry but that sounds a little bit like "the road traffic act states you may only drive 100mph but everyone else drives much faster... should I do that too?" Only because people don't follow best practices shouldn't mean their way is better
Robin Schambach
CC BY-SA 4.0
2018-08-29T15:51:28.373Z
Well "best practice" is a bit unclear to me here. Even though the docs mention CRUD *could* go in Services, Pixel & Tonic's own plugins and core classes sometimes put CRUD in Controllers. I'm wondering if there's a logic to this choice.
Lazlo
CC BY-SA 4.0
2018-08-29T15:58:16.770Z
As far as I know there are no controllers that contains direct CRUD logic in Craft core. They always call services for that
Robin Schambach
<plugin-development><craft3>
In Craft 3 plugins, should CRUD operations go in Controllers or Services?
# In Craft 3 plugins, should CRUD operations go in Controllers or Services?

(Asked by: username_0 on 2018-08-29)

The documentation states:

> Many service methods perform some sort of operation for a given model, such as a CRUD operation.

[(Link)](https://docs.craftcms.com/v3/extend/services.html#model-operation-methods)

But browsing through existing plugins, I see the CRUD logic is most often done in controllers.

What's the best practice here?

**Tags:** plugin-development, craft3
> **Comments:**
> **username_1**: Sorry but that sounds a little bit like "the road traffic act states you may only drive 100mph but everyone else drives much faster... should I do that too?" Only because people don't follow best practices shouldn't mean their way is better  
> **username_0**: Well "best practice" is a bit unclear to me here. Even though the docs mention CRUD *could* go in Services, Pixel & Tonic's own plugins and core classes sometimes put CRUD in Controllers. I'm wondering if there's a logic to this choice.  
> **username_1**: As far as I know there are no controllers that contains direct CRUD logic in Craft core. They always call services for that  

---

## 1 answers

### Answer by username_2 on 2018-08-29. Score: 4 | Accepted answer

Putting aside the term "CRUD" for a moment, it essentially breaks down like this...

* If something wants to ping your site, it is (almost) inevitably going to be pinging a Controller.
* If you want to do something in the database (create, update, delete), you should probably be doing that in a Service.

Which raises the question, *"how/where to bridge that gap?"*

The answer is, *"it depends."*

You could keep most of your logic in the Controller, and just move the DB interactions to the Service. Or you could immediately ping a Service method as soon as your Controller is hit. Either way is fine, and it depends heavily on what the site requires, and what you (as the developer) are more comfortable with.

As you noted, sometimes people bend the rules and put DB interactions directly into the Controller method. While that's frowned upon, there may not be any negative repercussions. So don't worry too much about how other people are handling it... just do what makes sense to you.


---

