# Controllers
Controllers are JavaScript files which contain a set of methods called actions reached by the client according to the requested route. It means that every time a client requests the route, the action performs the business logic code and sends back the response. They represent the C in the MVC pattern. In most cases, the controllers will contain the bulk of a project's business logic.
module.exports = {
// GET /hello
async index(ctx) {
return 'Hello World!';
},
};
In this example, any time a web browser is pointed to the /hello
URL on your app, the page will display the text: Hello World!
.
The controllers are defined in each ./api/**/controllers/
folder. Every JavaScript file put in these folders will be loaded as a controller. They are also available through the strapi.controllers
and strapi.api.**.controllers
global variables.
# Core controllers
When you create a new Content Type
you will see a new empty controller has been created. This is because Strapi builds a generic controller for your models by default and allows you to override and extend it in the generated files.
# Extending a Model Controller
Here are the core methods (and their current implementation). You can simply copy and paste this code in your own controller file to customize the methods.
β CAUTION
In the following example we will assume your controller, service and model are named restaurant
.
# Utils
First require the utility functions
const { parseMultipartData, sanitizeEntity } = require('strapi-utils');
parseMultipartData
: This function parses Strapi's formData format.sanitizeEntity
: This function removes all private fields from the model and its relations.
# Collection Type
# Single Type
# Custom controllers
You can also create custom controllers to build your own business logic and API endpoints.
There are two ways to create a controller:
- Using the CLI
strapi generate:controller restaurant
.
Read the CLI documentation for more information. - Manually create a JavaScript file in
./api/**/controllers
.
# Adding Endpoints
Each controllerβs action can be an async
or sync
function.
Every action receives a context
(ctx
) object as first parameter containing the request context and the response context.
# Example
In this example, we are defining a specific route in ./api/hello/config/routes.json
that takes hello.index
as handler. For more information on routing, please see the Routing documentation
It means that every time a request GET /hello
is sent to the server, Strapi will call the index
action in the hello.js
controller.
Our index
action will return Hello World!
. You can also return a JSON object.
Path β ./api/hello/config/routes.json
.
{
"routes": [
{
"method": "GET",
"path": "/hello",
"handler": "hello.index",
"config": {
"policies": []
}
}
]
}
Path β ./api/hello/controllers/hello.js
.
module.exports = {
// GET /hello
async index(ctx) {
ctx.send('Hello World!');
},
};
π‘ TIP
A route handler can only access the controllers defined in the ./api/**/controllers
folders.
β Policies Requests & Responses β