Handling routes with controllers
The custom routes definitions (in src/api/tutorial/routes/custom-tutorial.js
) have two main responsibilities:
- To define how the external world can call an API operation
- To map that operation to an internal method
The method we map to lives in a component called Controller
. This mapping is set in the handler
property in the route definition (they are referred to as actions in the Strapi documentation).
controller
encapsulates a set of methods that we can point to from our API endpoints. It's almost a one-to-one relationship since each route has a handler function (although in theory, nothing prevents us from pointing many routes to the same handler).
Each content-type has its controller set up in the module in src/api/{content-type}/controllers/{content-type}.js
. For the classroom
content-type, the controller is defined in src/api/classroom/controllers/classroom.js
.
Let's open the file and explore its contents...