Skip to content

Commit b5ef1b6

Browse files
committed
[docs] Add basic rules for routes
1 parent 411fdc9 commit b5ef1b6

File tree

1 file changed

+155
-0
lines changed
  • general/development/policies/routing

1 file changed

+155
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
title: Routing Policies
3+
tags:
4+
- Processes
5+
- Core development
6+
- Policy
7+
- Routing
8+
- Pages
9+
---
10+
11+
This document describes the policies for route naming in Moodle.
12+
13+
Routes are a powerful feature introduced in Moodle 4.5, and further extended in Moodle 5.0 which allow parts of Moodle, including plugins, to generate content without an explicit PHP endpoint file directly hit by users.
14+
15+
See the [Routing system documentation](/docs/apis/subsystems/routing/index) for further information.
16+
17+
Routes are currently used in the following places:
18+
19+
- The REST API
20+
- End user content (pages)
21+
- Shims to redirect legacy locations to new pages (shims)
22+
23+
Part of the URL used to access these pages is automatically generated by the component that the route is a part of, but the remaining part is generated by the developer.
24+
25+
To ensure consistency across Moodle, and to mitigate the risk of route shadowing, Moodle follows some rules for route paths.
26+
27+
Routes are typically comprised of:
28+
29+
- the component prefix
30+
- an _entity_ type, for example `course`
31+
- an entity _identifier_, for example `948`
32+
- an _action_ on that entity, for example `view`
33+
34+
Some of these parts may not be required, depending on the component and route type.
35+
36+
## The Component {#component-naming}
37+
38+
Where a component is present in the URL, the value is automatically determined from the location of the class defining the route.
39+
40+
:::tip Component Appearance
41+
42+
Where the component is a plugin, the full Frankenstyle Plugin Name is used, for example `mod_assign`.
43+
44+
Where the component is a core subsystem such as `core_course` then the `core_` prefix is removed, for example `course`.
45+
46+
:::
47+
48+
## Entity types
49+
50+
In many cases the component will be the entity, for example in the `core_course` component, the primary entity is the course. This is referred to as the _default_ entity.
51+
52+
The following are examples of a default entity:
53+
54+
``` title="The core_course subsystem has a default entity of 'course'"
55+
/rest/api/v2/course/<courseid>
56+
/course/<courseid>/view
57+
```
58+
59+
The following are examples of a non-default entity:
60+
61+
``` title="The core subsystem does not have any default entity"
62+
/rest/api/v2/core/templates/<theme>/<templateName>
63+
/rest/api/v2/core/strings/<language>/<stringComponent>/<stringIdentifier>
64+
```
65+
66+
:::danger Default and non-default entities
67+
68+
The use of both a _default_ entity _and_ a non-default entity is not officially supported.
69+
70+
:::
71+
72+
:::tip Singular or Plural
73+
74+
Where present, entities should always be in the plural form.
75+
76+
:::
77+
78+
## REST API Route Paths {#rest-route-paths}
79+
80+
All REST paths follow the following format:
81+
82+
```
83+
/rest/api/v2/<component>/<path>
84+
```
85+
86+
:::tip
87+
88+
The [standard rules](#component-naming) for `component` formatting apply.
89+
90+
:::
91+
92+
### Action verbs on the REST API
93+
94+
In most cases the REST API will make use of different HTTP Methods to dictate the relevant actions, for example:
95+
96+
- `GET` to fetch data
97+
- `POST` - create data
98+
- `DELETE` to delete data
99+
100+
Therefore the use of action verbs in REST API paths is strongly discouraged.
101+
102+
### Sub-entities on the REST API
103+
104+
In many cases an entity may have related child entities, or _sub-entities_. These should be placed after the primary or default entity's identifier, for example:
105+
106+
```
107+
/rest/api/v2/course/<courseid>/section/<sectionid>
108+
```
109+
110+
## Page Route Paths
111+
112+
Moodle 5.0 adds support for page routes. Page routes allow a standard Moodle page to be served without the user visiting a PHP page explicitly created for that purpose.
113+
114+
Page routes make use of the Routing engine to manage their complete end-to-end workflow.
115+
116+
All Page routes follow the following format:
117+
118+
```
119+
/<component>/<path>
120+
```
121+
122+
:::tip
123+
124+
The [standard rules](#component-naming) for `component` formatting apply.
125+
126+
:::
127+
128+
### Action verbs on Page Routes
129+
130+
In the case of Page Routes, it is not possible to use HTTP methods. Instead an action verb should be specified.
131+
132+
:::tip
133+
134+
The action should be specified _after_ the identifier, for example:
135+
136+
```
137+
/course/<identifier>/<action>
138+
```
139+
140+
:::
141+
142+
Action verbs should be single, simple, words and clearly identify the purpose of that action, for example:
143+
144+
- `view`
145+
- `edit`
146+
- `manage`
147+
- `delete`
148+
149+
### Sub-entities on Page Routes
150+
151+
In many cases an entity may have related child entities. These should be placed after the identifier of the primary entity, for example:
152+
153+
```
154+
/course/<courseid>/capabilities
155+
```

0 commit comments

Comments
 (0)