-
Notifications
You must be signed in to change notification settings - Fork 3
Route Class Documentation
The Route
class is part of the Webrium
namespace and is responsible for handling routing in web applications.
Route::source(array $route_file_names): void
This method takes an array of route file names and includes them from the routes
directory. Each route file should contain respective routing code for a specific part of the application.
In the Webrium framework, the method is used in the public/index.php
, and other path files can be added to it by editing it.
Example:
Route::source(['web.php', 'api.php']);
In this example, the web.php
and api.php
files will be included from the routes
directory.
These methods are used to define routes with corresponding HTTP verbs (GET
, POST
, PUT
, DELETE
). They take the URL path, handler (controller or closure), and optional route name as arguments.
Route::get(string $url, string|callable $handler, string $route_name = ''): void
Route::post(string $url, string|callable $handler, string $route_name = ''): void
Route::put(string $url, string|callable $handler, string $route_name = ''): void
Route::delete(string $url, string|callable $handler, string $route_name = ''): void
Route::any(string $url, string|callable $handler, string $route_name = ''): void
Example:
// route with GET method
Route::get('/users/{id}', function($id){
// implementation
}, 'user.show');
// route with POST method
Route::post('/users', 'UserController->store');
// route with PUT method
Route::put('/users/{id}', 'UserController->update');
// route with DELETE method
Route::delete('/users/{id}', 'UserController->delete');
The group method is used to categorize routes. You can define the same prefix for them. Middleware can also be defined for it to control user access to the routes.
The group method contains two parameters. The first parameter can receive a string type or an array including prefix and middleware properties, If the first parameter is a string, it is considered as a prefix. and a function is created in the second parameter to define the paths in it :
Route::group('admin', function(){
Route::get('post/list', 'IndexController->showPostList');
});
You can create the first parameter as an array and define the prefix
and middleware
for it :
function checkIsAdmin(){
return true;
}
Route::group(['prefix'=>'admin', 'middleware'=> 'checkIsAdmin' ], function(){
});
The middleware value can be of boolean
type or the name of the function or it can be created directly in that function
. Finally, when the value received by the middleware is true
, it gives access to the paths inside it
Also, middleware can contain an array
of values as mentioned above
Some more examples of group :
Route::group(['prefix'=>'admin', 'middleware'=> ['checkIsAdmin'] ], function(){
});
Route::group(['middleware' => function () {
// Middleware logic here
return true; // or false
}], function () {
// Routes for this group
});
Route::group(['middleware' => [$is_admin, $is_login]], function () {
// Routes for this group
});
This method takes a route name string and returns the URL of the corresponding route. If the route is not found, an error message will be generated.
Route::getRouteByName(string $route_name): string
Example:
$url = Route::getRouteByName('user.show');
// returns '/users/{id}'
This method sets the handler for when a page is not found (404 error). The handler can be a callable function or a string in the format of ControllerClass->methodName
.
Route::setNotFoundRoutePage(callable|string $handler): void
Example:
Route::setNotFoundRoutePage(function(){
echo "404 page not found";
});
In special cases, if we want to execute a method in a controller by ourselves, we can do this by using the executeControllerMethod method, which receives two parameters, the first parameter contains a string in the format ControllerClass->methodName, and the second parameter is optional. and if your method has parameters, you can define and value those parameters
Route::executeControllerMethod(string $handler_string, array $params = []): mixed
Example:
$result = Route::runController('UserController->show', ['id' => 1]);
This method implements the routing logic by matching the requested URL with the defined routes. If a match is found, the corresponding handler is executed. If no match is found, a 404 error message is displayed. This function is called by default in the Webrium framework
Route::run(): void