You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -219,6 +219,37 @@ When a new [content-type](/cms/backend-customization/models#content-types) is cr
219
219
To see a possible advanced usage for custom controllers, read the [services and controllers](/cms/backend-customization/examples/services-and-controllers) page of the backend customization examples cookbook.
220
220
:::
221
221
222
+
### Controllers & Routes: How routes reach controller actions
223
+
224
+
- Core mapping is automatic: when you generate a content-type, Strapi creates the matching controller and a router file that already targets the standard actions (`find`, `findOne`, `create`, `update`, and `delete`). Overriding any of these actions inside the generated controller does not require touching the router — the route keeps the same handler string and executes your updated logic.
225
+
- Adding a route should only be done for new actions or paths. If you introduce a brand-new method such as `exampleAction`, create or update a route entry whose `handler` points to the action so HTTP requests can reach it. Use the fully-qualified handler syntax `<scope>::<api-or-plugin-name>.<controllerName>.<actionName>` (e.g. `api::restaurant.restaurant.exampleAction` for an API controller or `plugin::menus.menu.exampleAction` for a plugin controller).
226
+
- Regarding controller and route filenames: the default controller name comes from the filename inside `./src/api/[api-name]/controllers/`. Core routers created with `createCoreRouter` adopt the same name, so the generated handler string matches automatically. Custom routers can follow any file naming scheme, as long as the `handler` string references an exported controller action.
227
+
228
+
The example below adds a new controller action and exposes it through a custom route without duplicating the existing CRUD route definitions:
Copy file name to clipboardExpand all lines: docusaurus/docs/cms/backend-customization/routes.md
+18-5Lines changed: 18 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,10 @@ Requests sent to Strapi on any URL are handled by routes. By default, Strapi gen
29
29
30
30
Once a route exists, reaching it executes some code handled by a controller (see [controllers documentation](/cms/backend-customization/controllers)). To view all existing routes and their hierarchal order, you can run `yarn strapi routes:list` (see [CLI reference](/cms/cli)).
31
31
32
+
:::tip
33
+
If you only customize the default controller actions (`find`, `findOne`, `create`, `update`, or `delete`) that Strapi generates for a content-type, you can leave the router as-is. Those core routes already target the same handler names and will run your new controller logic. Add or edit a route only when you need a brand-new HTTP path/method or want to expose a custom controller action.
34
+
:::
35
+
32
36
<figurestyle={{width:'100%',margin:'0'}}>
33
37
<imgsrc="/img/assets/backend-customization/diagram-routes.png"alt="Simplified Strapi backend diagram with routes highlighted" />
34
38
<em><figcaption style={{fontSize: '12px'}}>The diagram represents a simplified version of how a request travels through the Strapi back end, with routes highlighted. The backend customization introduction page includes a complete, <ahref="/cms/backend-customization#interactive-diagram">interactive diagram</a>.</figcaption></em>
@@ -174,7 +178,7 @@ Creating custom routers consists in creating a file that exports an array of obj
|`method`| Method associated to the route (i.e. `GET`, `POST`, `PUT`, `DELETE` or `PATCH`) |`String`|
176
180
|`path`| Path to reach, starting with a forward-leading slash (e.g. `/articles`)|`String`|
177
-
|`handler`| Function to execute when the route is reached.<br/>Should follow this syntax: `<controllerName>.<actionName>`|`String`|
181
+
|`handler`| Function to execute when the route is reached.<br/>Use the fully-qualified syntax`api::api-name.controllerName.actionName` (or `plugin::plugin-name.controllerName.actionName`). The short `<controllerName>.<actionName>` form for legacy projects also works.|`String`|
178
182
|`config`<br /><br />_Optional_| Configuration to handle [policies](#policies), [middlewares](#middlewares) and [public availability](#public-routes) for the route<br/><br/> |`Object`|
179
183
180
184
<br/>
@@ -185,6 +189,15 @@ Dynamic routes can be created using parameters and regular expressions. These pa
185
189
Routes files are loaded in alphabetical order. To load custom routes before core routes, make sure to name custom routes appropriately (e.g. `01-custom-routes.js` and `02-core-routes.js`).
186
190
:::
187
191
192
+
:::info Controller handler naming reference
193
+
The `handler` string acts as a pointer to the controller action that should run for the route. Strapi supports the following formats:
194
+
195
+
- API controllers: `api::<api-name>.<controllerName>.<actionName>` (e.g. `api::restaurant.restaurant.exampleAction`). The `<controllerName>` comes from the controller filename inside `./src/api/<api-name>/controllers/`.
196
+
- Plugin controllers: `plugin::<plugin-name>.<controllerName>.<actionName>` when the controller lives in a plugin.
197
+
198
+
For backwards compatibility, Strapi also accepts a short `<controllerName>.<actionName>` string for API controllers, but using the fully-qualified form makes the route more explicit and avoids naming collisions across APIs and plugins.
199
+
:::
200
+
188
201
<details>
189
202
190
203
<summary>Example of a custom router using URL parameters and regular expressions for routes</summary>
@@ -202,12 +215,12 @@ module.exports = {
202
215
{ // Path defined with an URL parameter
203
216
method:'POST',
204
217
path:'/restaurants/:id/review',
205
-
handler:'restaurant.review',
218
+
handler:'api::restaurant.restaurant.review',
206
219
},
207
220
{ // Path defined with a regular expression
208
221
method:'GET',
209
222
path:'/restaurants/:category([a-z]+)', // Only match when the URL parameter is composed of lowercase letters
0 commit comments