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

88 lines
2.3 KiB
Markdown
Raw Normal View History

# Controllers
See the [controllers' concepts](../concepts/concepts.md#controllers) for details.
2019-05-16 11:14:32 +02:00
## Core controllers
When you create a new Content type or a new model. You will see a new empty controller as been created. It is because Strapi builds a generic api 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 there current implementation).
You can simply copy and paste this code in your own controller file to customize the methods.
#### `find`
```js
module.exports = {
/**
* Retrieve records.
*
* @return {Object|Array}
*/
find(ctx) {
if (ctx.query._q) {
return service.search(ctx.query);
}
return service.find(ctx.query);
},
};
```
## Custom controllers
You can also add controller outisde of Content Types to build custom business logic and Api endpoints.
### Creating a custom controller?
There are two ways to create a controller:
2019-05-16 11:14:32 +02:00
- Using the CLI `strapi generate:controller products`. Read the [CLI documentation](../cli/CLI.md#strapi-generatecontroller) for more information.
- 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.
:::