Overview
TheRouter class handles HTTP request routing in Lyger. It supports dynamic route parameters, controller-based routing, and automatic dependency injection through the container.
Constructor
Create a new Router instance with dependency injection support.Container
required
Container instance for dependency injection
Example
Route Registration
registerRoute
Register a route with a specific HTTP method and handler.string
required
HTTP method (GET, POST, PUT, DELETE)
string
required
Route path pattern, supports dynamic parameters with
{param} syntaxcallable|array
required
Route handler - can be a closure or controller array
[ControllerClass, 'method']Example
- Closure Handler
- Controller Handler
- Multiple Parameters
loadRoutesFromFile
Load routes from a PHP file using the Route facade.string
required
Path to the routes file
Example
Request Dispatching
dispatch
Match a request to a registered route and execute its handler.Request
required
The HTTP request to dispatch
Returns
Response
Response object from the matched handler, or 404 error if no route matches
The router automatically passes the Request object as the first parameter to all handlers, followed by any route parameters.
Example
Route Facade
TheRoute class provides a convenient facade for defining routes that can be loaded by the router.
Static Methods
- GET
- POST
- PUT
- DELETE
Route Parameters
Routes support dynamic parameters using curly brace syntax{param}.
Single Parameter
Multiple Parameters
Controller Integration
Controllers are automatically instantiated with dependency injection when using array handlers.Example
Response Formatting
The router automatically formats handler return values:- Response Object
- Array
- String
Error Handling
The router handles errors gracefully:- 404 Not Found: Returned when no route matches the request
- 500 Internal Server Error: Returned when handler execution fails