Skip to main content
The Router class manages HTTP method routing, parses registered routes, and handles the current requested URI. It provides a fluent interface for defining routes and middleware.

Static route methods

Define routes using static HTTP method calls.

get()

Register a GET route.
Router::get('/users', [UserController::class, 'index']);
Router::get('/users/{id}', [UserController::class, 'show']);
route
string
required
The URI pattern to match (supports parameters like {id})
handler
callable|string
required
Callback function or controller method to handle the request
return
Route
Returns a Route instance for method chaining

post()

Register a POST route.
Router::post('/users', [UserController::class, 'store']);
route
string
required
The URI pattern to match
handler
callable|string
required
Callback function or controller method
return
Route
Returns a Route instance

put()

Register a PUT route.
Router::put('/users/{id}', [UserController::class, 'update']);
route
string
required
The URI pattern to match
handler
callable|string
required
Callback function or controller method
return
Route
Returns a Route instance

patch()

Register a PATCH route.
Router::patch('/users/{id}', [UserController::class, 'patch']);
route
string
required
The URI pattern to match
handler
callable|string
required
Callback function or controller method
return
Route
Returns a Route instance

delete()

Register a DELETE route.
Router::delete('/users/{id}', [UserController::class, 'destroy']);
route
string
required
The URI pattern to match
handler
callable|string
required
Callback function or controller method
return
Route
Returns a Route instance

Instance methods

addRoute()

Register a Route object manually.
$route = new Route('/api/users', [UserController::class, 'index']);
app()->router->addRoute('GET', $route);
method
string
required
HTTP method (GET, POST, PUT, PATCH, DELETE)
route
Route
required
Route instance to register
return
Route
Returns the registered Route instance

dispatch()

Dispatch the content for the current route and method.
app()->router->dispatch();
return
mixed
Returns the handler response content

getRoute()

Get the current route being processed.
$currentRoute = app()->router->getRoute();
return
Route
Returns the current Route instance

match()

Confirm if a URI and HTTP method match any registered route.
$route = app()->router->match('GET', '/users/123');
method
string
required
HTTP method to match
uri
string
required
URI to match against registered routes
return
bool|Route
Returns Route instance if matched, false otherwise

getUriParts()

Parse a URI and return its parts and subdomain.
$parts = app()->router->getUriParts('/users/123');
// Returns: ['parts' => ['users', '123'], 'subdomain' => '*']
uri
string
required
URI to parse
return
array
Array containing ‘parts’ and ‘subdomain’ keys

parseRoute()

Deconstruct a route to extract URI parts and subdomain.
app()->router->parseRoute($route);
route
Route
required
Route instance to parse
return
void
Modifies the route object in place

getRoutes()

Get all registered routes, optionally filtered by method and subdomain.
$routes = app()->router->getRoutes('GET', '*');
method
string
default:""
HTTP method to filter by
subdomain
string
default:"*"
Subdomain to filter by
return
array
Array of registered routes

Static utility methods

group()

Group routes to apply middleware to multiple routes.
Router::group('auth,web', function() {
    Router::get('/dashboard', [DashboardController::class, 'index']);
    Router::get('/profile', [ProfileController::class, 'show']);
});
middlewares
string|array
required
Comma-separated string or array of middleware names
callback
callable
required
Callback function containing route definitions
return
void
Routes defined in callback will have middleware applied

runMiddlewares()

Execute an array of middleware classes.
Router::runMiddlewares([AuthMiddleware::class, CorsMiddleware::class]);
middlewares
array
required
Array of middleware class names
return
void
Executes each middleware in sequence

loadRequestedRoutes()

Load routes based on the current environment and URI.
Router::loadRequestedRoutes();
return
void
Loads appropriate route files for web or CLI environment

validateRequestedRoutesByFile()

Load routes from a specific route file.
Router::validateRequestedRoutesByFile('api');
routeFile
string
default:"web"
Name of the route file to load (without .php extension)
return
bool
Returns true if routes were loaded successfully

Examples

Basic routing

Router::get('/', function() {
    return view('home');
});

Router::post('/users', [UserController::class, 'store']);

Route parameters

Router::get('/users/{id}', function($id) {
    return User::find($id);
});

Router::get('/posts/{slug}/comments/{comment}', [
    CommentController::class, 'show'
]);

Subdomain routing

// Routes for admin.domain.com
Router::get('admin@/dashboard', [AdminController::class, 'dashboard']);

Middleware groups

Router::group('auth', function() {
    Router::get('/dashboard', [DashboardController::class, 'index']);
    Router::post('/logout', [AuthController::class, 'logout']);
});

Chained middleware

Router::get('/admin', [AdminController::class, 'index'])
    ->withMiddleware([AuthMiddleware::class, AdminMiddleware::class]);

Build docs developers (and LLMs) love