Route mapping is the recommended modern approach for creating routes in web applications. The map()
function is utilized to create new routes and append them to their respective actions.
The basic syntax for route mapping is:
Route::map()
The map function accepts two parameters:
- Request Method Parameter: Defines the HTTP request method for each instance
- Route URL Parameter: Defines the actual route URL path
The following HTTP request methods are supported:
- GET
- POST
- PUT
- PATCH
- DELETE
Multiple methods can be defined for each route instance by separating them with '|':
"GET|POST" // This will match both GET and POST requests
Route mapping supports custom pattern matching for request URIs. Patterns are declared using the following syntax:
Route::map(GET, 'pattern: {{your_pattern}}')
- Must start with
pattern:
keyword - Uses fnmatch() style pattern matching
- Common patterns:
pattern: *
- Matches all request URIspattern: /admin/*
- Matches any URI that starts with /admin/
Example:
Route::map(GET, 'pattern: /users/*')->action(function() {
// Handles any URI starting with /users/
});
You can map additional routes to a base route, creating a hierarchical structure:
Route::map(GET, "/admin")->action()
->Route::route("/dashboard", function(Request $req, Closure $accept) {
$accept(GET, fn($method) => "`$method` method is not allowed");
return "Admin Dashboard";
})
- First parameter: Request URI path
- Second parameter: Closure or callable function
- The closure receives two arguments:
$req
: Request header information$accept
: Function to handle method acceptance
- The closure receives two arguments:
- Used to specify allowed HTTP methods
- Takes two parameters:
- Allowed method(s)
- Fallback function for unallowed methods
- The fallback receives the actual request method as an argument
Example accessing nested route:
http://localhost:2200/admin/dashboard
By default, all routes are case insensitive. To make a route case sensitive, use the caseSensitive()
method:
Route::map(GET, "/Admin")->caseSensitive()->action(function() {
// This route will only match exact case "/Admin"
});
- Case insensitive:
/admin
will match/Admin
,/ADMIN
, etc. - With
caseSensitive()
: Only exact case matches will work
- Takes 1 parameter which specifies the view file path to be rendered
- Used for direct view file rendering
- Example:
Route::map("GET", "/index")->file("::index");
- Used in adding controller class methods
- Requires controller class name followed by double colon and method name
- Example:
Route::map(GET, "/index")->use("UserController::index");
Note: When following the MVC pattern, you don't need to specify the controller namespace.
- Called directly with the response to be rendered
- Supports multiple formats:
Route::map()->action([Controller::class, "method"]);
Route::map()->action(function(){ return "Hello"; });
Route::map()->action(function(Request $req) { ... });
Route::map()->action("Hello World");
You can call functional arrays using:
Route::map()->action([Controller::class, "method"]);
This will execute the method function in that class and display it on the web.
When a default string is passed as an argument in the parameter:
Route::map()->action(view::render("::index"));
The view file will be the output when they navigate to the route.
For basic string output:
Route::map()->action("Hello World");
This will display "Hello World" on the web page.
- For each function called, the Request will be passed as the argument in the parameters if set
- By default, it will output any result it is given
- Views are typically rendered using the view function:
View::render("Folder::File")