strapi/docs/3.0.0-beta.x/guides/controllers.md

187 lines
3.8 KiB
Markdown
Raw Normal View History

# Controllers
2019-05-16 11:35:19 +02:00
See the [controllers' concepts](../concepts/concepts.md#controllers) for a simple overview.
2019-05-16 11:14:32 +02:00
## Core controllers
2019-05-16 15:00:55 +02:00
When you create a new Content type or a new model. You will see a new empty controller has been created. It is because Strapi builds a generic controller for your models by default and allows you to override and extend it in the generated files.
2019-05-16 11:14:32 +02:00
### Extending a Model Controller
2019-05-16 15:00:55 +02:00
Here are the core methods (and their current implementation).
2019-05-16 11:14:32 +02:00
You can simply copy and paste this code in your own controller file to customize the methods.
2019-05-16 11:35:19 +02:00
::: warning
In the following example we will consider your controller, service and model is named `product`
:::
2019-05-16 11:14:32 +02:00
#### `find`
```js
module.exports = {
/**
* Retrieve records.
*
2019-05-16 11:35:19 +02:00
* @return {Array}
2019-05-16 11:14:32 +02:00
*/
find(ctx) {
if (ctx.query._q) {
2019-05-16 11:35:19 +02:00
return strapi.services.product.search(ctx.query);
2019-05-16 11:14:32 +02:00
}
2019-05-16 11:35:19 +02:00
return strapi.services.product.find(ctx.query);
},
};
```
#### `findOne`
```js
module.exports = {
/**
* Retrieve a record.
*
* @return {Object}
*/
findOne(ctx) {
return strapi.services.product.findOne(ctx.params);
},
};
```
#### `count`
```js
module.exports = {
/**
* Count records.
*
* @return {Number}
*/
count(ctx) {
if (ctx.query._q) {
return strapi.services.product.countSearch(ctx.query);
}
return strapi.services.product.count(ctx.query);
},
};
```
#### `create`
```js
module.exports = {
/**
* Create a record.
*
* @return {Object}
*/
create(ctx) {
2019-08-12 16:58:18 +02:00
if (ctx.is('multipart')) {
2019-08-13 10:41:11 +02:00
// Parses strapi's formData format
2019-08-12 16:58:18 +02:00
const { data, files } = this.parseMultipartData(ctx);
return service.create(data, { files });
}
return service.create(ctx.request.body);
2019-05-16 11:35:19 +02:00
},
};
```
#### `update`
```js
module.exports = {
/**
* Update a record.
*
* @return {Object}
*/
update(ctx) {
2019-08-12 16:58:18 +02:00
if (ctx.is('multipart')) {
2019-08-13 10:41:11 +02:00
// Parses strapi's formData format
2019-08-12 16:58:18 +02:00
const { data, files } = this.parseMultipartData(ctx);
return service.update(ctx.params, data, { files });
}
return service.update(ctx.params, ctx.request.body);
2019-05-16 11:35:19 +02:00
},
};
```
#### `delete`
```js
module.exports = {
/**
* delete a record.
*
* @return {Object}
*/
delete(ctx) {
return strapi.services.product.delete(ctx.params);
2019-05-16 11:14:32 +02:00
},
};
```
## Custom controllers
2019-05-16 11:35:19 +02:00
You can also create custom controllers to build your own business logic and API endpoints.
2019-05-16 11:14:32 +02:00
2019-05-16 15:00:55 +02:00
### How to create a custom controller
There are two ways to create a controller:
2019-05-16 15:00:55 +02:00
- Using the CLI `strapi generate:controller product`. Read the [CLI documentation](../cli/CLI.md#strapi-generatecontroller) for more information.
2019-05-16 11:14:32 +02:00
- Manually create a JavaScript file in `./api/**/controllers`.
### Adding Endpoints
Each controllers action must be an `async` function.
Every action receives a `context` (`ctx`) object as first parameter containing the [request context](../guides/requests.md) and the [response context](../guides/responses.md).
2019-05-16 11:14:32 +02:00
::: note
Every action must be referenced by a route.
:::
### Example
2019-05-16 11:14:32 +02:00
In this example, we are defining a specific route in `./api/hello/config/routes.json` that takes `Hello.index` as handler.
2019-05-16 11:14:32 +02:00
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`.
2019-05-16 11:14:32 +02:00
```json
{
"routes": [
{
"method": "GET",
"path": "/hello",
"handler": "Hello.index"
}
]
}
```
**Path —** `./api/hello/controllers/Hello.js`.
2019-05-16 11:14:32 +02:00
```js
module.exports = {
// GET /hello
2019-05-16 11:14:32 +02:00
index: async ctx => {
ctx.send('Hello World!');
2019-05-16 11:14:32 +02:00
},
};
```
::: note
A route handler can only access the controllers defined in the `./api/**/controllers` folders.
:::