Routing
Table of contents
1. Overview Table of Contents
Routing System (Dynamic Routing) Unlike many modern PHP frameworks that use a route definition file (e.g., routes/web.php), Chappy.php uses dynamic routing. This means routes are automatically resolved based on the URL structure and available controllers/methods—no manual route registration required.
2. How It Works Table of Contents
The Router class parses incoming requests and maps them directly to controller methods using the URL pattern:
/controller/method/optional/params
For example:
| URL | Resolved Method |
|------------------------|-------------------------------------------|
| `/home/index` | `HomeController::index()` |
| `/user/profile/42` | `UserController::profile(42)` |
| `/auth/login` | `AuthController::login()` |
If the URL is /, it defaults to:
HomeController::index()
3. Default Conventions Table of Contents
- Controller classes must be located in
app/Controllers/. - Controller names should end with
Controller(e.g.,UserController). - Method names in controllers map 1:1 with URL segments.
- Additional URL segments are passed as arguments to the controller method.
4. Fallbacks Table of Contents
If a controller does not exist the user is redirected to a view indicating the issue. When an action does not exist then whoops displays an error indicating the function does not exist in the controller.
5. No Route File? Why? Table of Contents
Chappy.php uses dynamic routing to keep your application lightweight, fast, and convention-driven, eliminating the need to register every route manually.
- This pattern is ideal for:
- Small to medium-sized applications
- Developers who prefer convention over configuration
- Rapid prototyping and reduced boilerplate
6. Pros and Cons Table of Contents
Pros
- No route files to maintain
- Easy to follow MVC conventions
- Clear structure: URL = Controller → Method
Considerations
- You can’t assign route names or middleware per route (yet)
- Custom route aliases or regex pattern matching aren’t supported out of the box
- You should avoid duplicate method names across controllers that could cause confusion
7. Redirect Table of Contents
This function comes with a static redirect function that can be called in two different ways.
Router::redirect('admindashboard.details', [$user->id])- Directly calls static functionredirect('admindashboard.details', [$user->id])- Shorthand publicly available global function