Skip to content

Commit a77d737

Browse files
committed
Merge branch 'main' of github.com:h3ravel/framework
2 parents dbefa87 + 968a661 commit a77d737

1 file changed

Lines changed: 361 additions & 0 deletions

File tree

Lines changed: 361 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
# Route List Command
2+
3+
The `musket route:list` command displays all registered routes in your H3ravel application, providing a comprehensive overview of your application's routing structure.
4+
5+
## Usage
6+
7+
```bash
8+
# Display all routes
9+
npx musket route:list
10+
11+
# Output as JSON format
12+
npx musket route:list --json
13+
14+
# Reverse the route ordering
15+
npx musket route:list --reverse
16+
npx musket route:list -r
17+
```
18+
19+
## Command Options
20+
21+
| Option | Shortcut | Description |
22+
|--------|----------|-------------|
23+
| `--json` | - | Output the route list in JSON format |
24+
| `--reverse` | `-r` | Reverse the ordering of the routes |
25+
26+
## Output Format
27+
28+
The command displays routes in a clean, tabular format with the following information:
29+
30+
### Table Layout
31+
32+
```
33+
METHOD|ALT PATH NAME › CONTROLLER@METHOD
34+
GET|HEAD / › HomeController@index
35+
POST /users users.store › UserController@store
36+
GET|HEAD /users/:user users.show › UserController@show
37+
PUT|PATCH /users/:user users.update › UserController@update
38+
DELETE /users/:user users.destroy › UserController@destroy
39+
GET|HEAD /hello hello.route ›
40+
GET|HEAD /mail › MailController@send
41+
GET|HEAD /app ›
42+
```
43+
44+
### Column Details
45+
46+
1. **METHOD|ALT**: HTTP method with alternative methods
47+
- `GET|HEAD` - GET requests also accept HEAD
48+
- `PUT|PATCH` - PUT requests also accept PATCH
49+
- `POST`, `DELETE` - Single method only
50+
51+
2. **PATH**: The route URL pattern
52+
- `/` - Root route
53+
- `/users` - Simple path
54+
- `/users/:user` - Path with parameters (shown in yellow)
55+
- Path segments with parameters are highlighted
56+
57+
3. **NAME › CONTROLLER@METHOD**: Route identification
58+
- `users.store` - Named route
59+
- `HomeController@index` - Controller and method
60+
- `` - Separator between name and controller
61+
- Empty if route uses closure/anonymous function
62+
63+
### Color Coding
64+
65+
The command uses color coding to enhance readability:
66+
67+
- **GET methods**: Blue
68+
- **POST methods**: Yellow
69+
- **PUT methods**: Yellow
70+
- **DELETE methods**: Red
71+
- **HEAD methods**: Gray
72+
- **Route parameters**: Yellow (e.g., `:user`, `:id`)
73+
- **Path separators**: White
74+
75+
## Examples
76+
77+
### Basic Route Listing
78+
79+
```bash
80+
npx musket route:list
81+
```
82+
83+
**Sample Output:**
84+
```
85+
GET|HEAD / › HomeController@index
86+
GET|HEAD /mail › MailController@send
87+
GET|HEAD /app ›
88+
GET|HEAD /users users.index › UserController@index
89+
POST /users users.store › UserController@store
90+
GET|HEAD /users/:user users.show › UserController@show
91+
PUT|PATCH /users/:user users.update › UserController@update
92+
DELETE /users/:user users.destroy › UserController@destroy
93+
GET|HEAD /hello hello.route ›
94+
```
95+
96+
### JSON Output
97+
98+
```bash
99+
npx musket route:list --json
100+
```
101+
102+
**Sample Output:**
103+
```json
104+
[
105+
{
106+
"method": "get",
107+
"path": "/",
108+
"name": null,
109+
"signature": ["HomeController", "index"]
110+
},
111+
{
112+
"method": "post",
113+
"path": "/users",
114+
"name": "users.store",
115+
"signature": ["UserController", "store"]
116+
},
117+
{
118+
"method": "get",
119+
"path": "/users/:user",
120+
"name": "users.show",
121+
"signature": ["UserController", "show"]
122+
}
123+
]
124+
```
125+
126+
### Reverse Ordering
127+
128+
```bash
129+
npx musket route:list --reverse
130+
```
131+
132+
Routes will be displayed in reverse alphabetical order by path.
133+
134+
## Route Types
135+
136+
### Controller Routes
137+
138+
Routes that point to controller methods:
139+
140+
```typescript
141+
// In routes/web.ts
142+
Route.get('/', [HomeController, 'index'])
143+
Route.post('/users', [UserController, 'store'])
144+
```
145+
146+
**Output:**
147+
```
148+
GET|HEAD / › HomeController@index
149+
POST /users › UserController@store
150+
```
151+
152+
### Named Routes
153+
154+
Routes with explicit names for URL generation:
155+
156+
```typescript
157+
// In routes/api.ts
158+
Route.get('/hello', () => 'Hello', 'hello.route')
159+
```
160+
161+
**Output:**
162+
```
163+
GET|HEAD /hello hello.route ›
164+
```
165+
166+
### Closure Routes
167+
168+
Anonymous function routes:
169+
170+
```typescript
171+
// In routes/web.ts
172+
Route.get('/app', async function () {
173+
return await view('index', { /* data */ })
174+
})
175+
```
176+
177+
**Output:**
178+
```
179+
GET|HEAD /app ›
180+
```
181+
182+
### Resource Routes
183+
184+
API resource routes (generated via `apiResource`):
185+
186+
```typescript
187+
// In routes/api.ts
188+
Route.apiResource('/users', UserController)
189+
```
190+
191+
**Output:**
192+
```
193+
GET|HEAD /users users.index › UserController@index
194+
POST /users users.store › UserController@store
195+
GET|HEAD /users/:user users.show › UserController@show
196+
PUT|PATCH /users/:user users.update › UserController@update
197+
DELETE /users/:user users.destroy › UserController@destroy
198+
```
199+
200+
### Grouped Routes
201+
202+
Routes defined within route groups:
203+
204+
```typescript
205+
// In routes/api.ts
206+
Route.group({
207+
prefix: '/admin',
208+
middleware: [AuthMiddleware]
209+
}, () => {
210+
Route.get('/dashboard', [AdminController, 'dashboard'])
211+
Route.apiResource('/users', AdminUserController)
212+
})
213+
```
214+
215+
**Output:**
216+
```
217+
GET|HEAD /admin/dashboard › AdminController@dashboard
218+
GET|HEAD /admin/users admin.users.index › AdminUserController@index
219+
POST /admin/users admin.users.store › AdminUserController@store
220+
```
221+
222+
## Integration with Controllers
223+
224+
### Controller Method Resolution
225+
226+
The route list shows how routes connect to controller methods:
227+
228+
1. **Class Name**: The controller class (e.g., `UserController`)
229+
2. **Method Name**: The method to be called (e.g., `index`, `store`, `show`)
230+
3. **Route Model Binding**: Parameters like `:user` automatically resolve to model instances
231+
232+
### Controller Dependencies
233+
234+
Controllers can use dependency injection:
235+
236+
```typescript
237+
export class UserController {
238+
constructor(private app: Application) {}
239+
240+
async show(ctx: HttpContext, user: User) {
241+
// :user parameter automatically resolved to User model
242+
return user
243+
}
244+
}
245+
```
246+
247+
The route list will show: `users.show › UserController@show`
248+
249+
### Middleware Integration
250+
251+
Routes can have middleware applied:
252+
253+
```typescript
254+
Route.group({
255+
middleware: [AuthMiddleware]
256+
}, () => {
257+
Route.apiResource('/users', UserController, [new AuthMiddleware()])
258+
})
259+
```
260+
261+
While middleware isn't shown in the route list output, it's applied during route resolution.
262+
263+
## Route Filtering
264+
265+
### Default Filtering
266+
267+
The command automatically filters out certain HTTP methods:
268+
269+
- **HEAD routes**: Not displayed separately (shown as `GET|HEAD`)
270+
- **PATCH routes**: Not displayed separately (shown as `PUT|PATCH`)
271+
272+
This reduces clutter while showing all available methods for each route.
273+
274+
### Path Ordering
275+
276+
Routes are sorted alphabetically by path with special handling:
277+
278+
1. **Root route (`/`)**: Always appears first
279+
2. **Other routes**: Sorted alphabetically
280+
3. **Parameter routes**: Parameters highlighted in yellow
281+
282+
## Practical Usage
283+
284+
### Development Workflow
285+
286+
```bash
287+
# Check all available routes
288+
npx musket route:list
289+
290+
# Find specific route patterns
291+
npx musket route:list | grep users
292+
293+
# Export routes for documentation
294+
npx musket route:list --json > routes.json
295+
```
296+
297+
### Debugging Routes
298+
299+
When routes aren't working as expected:
300+
301+
1. **Verify route registration**: Check if route appears in the list
302+
2. **Check method matching**: Ensure HTTP method is correct
303+
3. **Validate path patterns**: Confirm parameter syntax
304+
4. **Controller resolution**: Verify controller and method names
305+
306+
### API Documentation
307+
308+
Use the JSON output to generate API documentation:
309+
310+
```bash
311+
# Generate route data for documentation tools
312+
npx musket route:list --json | jq '.[] | select(.method == "get")'
313+
```
314+
315+
## Common Route Patterns
316+
317+
### RESTful Resources
318+
319+
```typescript
320+
Route.apiResource('/users', UserController)
321+
```
322+
323+
Generates standard REST routes:
324+
- `GET /users` - List all users
325+
- `POST /users` - Create new user
326+
- `GET /users/:user` - Show specific user
327+
- `PUT /users/:user` - Update user
328+
- `DELETE /users/:user` - Delete user
329+
330+
### Nested Resources
331+
332+
```typescript
333+
Route.apiResource('/users/:user/posts', PostController)
334+
```
335+
336+
Creates nested resource routes:
337+
- `GET /users/:user/posts` - User's posts
338+
- `POST /users/:user/posts` - Create post for user
339+
340+
### Custom Actions
341+
342+
```typescript
343+
Route.post('/users/:user/activate', [UserController, 'activate'])
344+
Route.post('/users/:user/deactivate', [UserController, 'deactivate'])
345+
```
346+
347+
## Best Practices
348+
349+
### Route Organization
350+
351+
1. **Logical grouping**: Use route groups for related functionality
352+
2. **Consistent naming**: Follow RESTful conventions for resource routes
353+
3. **Meaningful names**: Provide descriptive route names for URL generation
354+
355+
### Performance Considerations
356+
357+
1. **Route ordering**: More specific routes should come before generic ones
358+
2. **Parameter validation**: Use middleware for parameter validation
359+
3. **Caching**: Consider route caching for large applications
360+
361+
The `route:list` command is an essential tool for understanding and debugging your H3ravel application's routing structure, providing clear visibility into how URLs map to your application logic.

0 commit comments

Comments
 (0)