<p>The short answer is ... you don't. There will never be anything like <code>controllers</code> in your templates folder, because that folder should only contain template files.</p>
<p>In order to use custom logic in Craft ('s templates), use a <a href="https://craftcms.com/docs/3.x/extend/module-guide.html" rel="nofollow noreferrer">module</a>. You could look at modules as a way to build your own little website-specific plugin, but without all the hassle of building a real plugin (your module will never be published to the plugin store, for instance). In fact, the default installation of Craft comes with a <code>module</code> folder, to get you going.</p>
<p>Modules, just like <a href="https://craftcms.com/docs/3.x/extend/plugin-guide.html" rel="nofollow noreferrer">plugins</a>, can have <a href="https://craftcms.com/docs/3.x/extend/controllers.html" rel="nofollow noreferrer">controllers</a>, <a href="https://craftcms.com/docs/3.x/extend/services.html" rel="nofollow noreferrer">services</a>, and more. They can even provide <a href="https://craftcms.com/docs/3.x/extend/template-roots.html" rel="nofollow noreferrer">template variables</a> so your module's logic can be used in your templates.</p>
<p>If you'd like to learn more about creating a module, please read the '<a href="https://craftcms.com/docs/3.x/extend/plugin-guide.html" rel="nofollow noreferrer">building a plugin</a>' documentation and read 'module' when the documentation says 'plugin'. Most things are pretty much the same.</p>

CC BY-SA 4.0
2021-09-21T15:15:00.563Z
Thanks Paul, this is a great answer.
Álvaro Franz
CC BY-SA 4.0
2021-09-22T08:57:35.163Z
Just a note: building a custom plugin or a module take the same effort (you don't need to submit your plugin to the store) and plugins offer useful features such as the ability to have a settings page in the control panel.
gioppe
CC BY-SA 4.0
2021-09-21T14:57:03.930Z
2021-09-21T14:57:03.930Z
Paul
<p>I come from a &quot;traditional&quot; PHP paradigm where views are mixed with processing logic. And now switching from WordPress to CraftCMS (which so far is a beautiful experience) I am having trouble with building custom logic.</p>
<p>For example, something as easy as integrating a backend for a webhook endpoint or processing a form is easily done when the &quot;view&quot; is mixed with the &quot;controller&quot;, as in WordPress themes.</p>
<p>I like the fact that CraftCMS uses twig to avoid this mixup (I see many advantages in this approach), but I haven't found documentation regarding this topic.</p>
<p>We have a folder with all the index.twig, content.twig ... but no &quot;controllers&quot; folder.</p>
<p>How do we add PHP logic to a &quot;template&quot; view?</p>

<php><controller><logic>
How to handle custom PHP logic in CraftCMS
# How to handle custom PHP logic in CraftCMS

(Asked by: username_0 on 2021-09-21)

I come from a "traditional" PHP paradigm where views are mixed with processing logic. And now switching from WordPress to CraftCMS (which so far is a beautiful experience) I am having trouble with building custom logic.

For example, something as easy as integrating a backend for a webhook endpoint or processing a form is easily done when the "view" is mixed with the "controller", as in WordPress themes.

I like the fact that CraftCMS uses twig to avoid this mixup (I see many advantages in this approach), but I haven't found documentation regarding this topic.

We have a folder with all the index.twig, content.twig ... but no "controllers" folder.

How do we add PHP logic to a "template" view?

**Tags:** php, controller, logic

---

## 1 answers

### Answer by username_1 on 2021-09-21. Score: 3 | Accepted answer

The short answer is ... you don't. There will never be anything like `controllers` in your templates folder, because that folder should only contain template files.

In order to use custom logic in Craft ('s templates), use a [module](https://craftcms.com/docs/3.x/extend/module-guide.html). You could look at modules as a way to build your own little website-specific plugin, but without all the hassle of building a real plugin (your module will never be published to the plugin store, for instance). In fact, the default installation of Craft comes with a `module` folder, to get you going.

Modules, just like [plugins](https://craftcms.com/docs/3.x/extend/plugin-guide.html), can have [controllers](https://craftcms.com/docs/3.x/extend/controllers.html), [services](https://craftcms.com/docs/3.x/extend/services.html), and more. They can even provide [template variables](https://craftcms.com/docs/3.x/extend/template-roots.html) so your module's logic can be used in your templates.

If you'd like to learn more about creating a module, please read the '[building a plugin](https://craftcms.com/docs/3.x/extend/plugin-guide.html)' documentation and read 'module' when the documentation says 'plugin'. Most things are pretty much the same.

> **Comments:**
> **username_0**: Thanks Paul, this is a great answer.  
> **username_2**: Just a note: building a custom plugin or a module take the same effort (you don't need to submit your plugin to the store) and plugins offer useful features such as the ability to have a settings page in the control panel.  

---

