Namespace: TrishulApi\Core\Http
Author: Shyam Dubey
Since: v1.0.0
Version: v1.0.0
The Router
class is the core of the Trishul API framework's routing system. It allows you to define HTTP routes, group them, apply middlewares, and control the request/response lifecycle in a structured and extensible way.
/user/{id}
).$url
(string): The route URL (e.g., "/user/{id}"
).$callback
(string): The handler in Class@method
format.$middlewares
(array): Optional array of middleware class names.array
(route definition)
get()
.
array
get()
.
array
get()
.
array
get()
.
array
/users
).
$url
(string): Parent URL (e.g., "/users"
).$childrens
(array): Array of child routes (use children()
method).$middlewares
(array): Middlewares for all children.$except
(array): Exempted routes (e.g., ["/users/login" => RequestType::POST]
).parent()
.
$method
(string): HTTP method (e.g., "GET"
).$url
(string): Child URL (e.g., "/all"
).$callback
(string): Handler in Class@method
format.array
$routes
(array): Array of route definitions.$middlewares
(array|string): Middleware class names.$except
(array): Routes to exempt (e.g., ["/login" => RequestType::POST]
).index.php
) after defining all routes.
array
use TrishulApi\Core\Http\Router; use App\Controller\UserController; use App\Middleware\AuthMiddleware; // Register a GET route Router::get("/user/{id}", UserController::class . "@getUser", [AuthMiddleware::class], "Get user", "Returns user by ID"); // Register a POST route Router::post("/user", UserController::class . "@createUser", [AuthMiddleware::class], "Create user", "Creates a new user"); // Register a DELETE route Router::delete("/user/{id}", UserController::class . "@deleteUser", [AuthMiddleware::class]); // Register a PUT route Router::put("/user/{id}", UserController::class . "@updateUser", [AuthMiddleware::class]); // Set global middlewares (applies to all routes except login/register) Router::set_global_middlewares( [AuthMiddleware::class, 'LoggingMiddleware'], ["/login" => "POST", "/register" => "POST"] ); // Grouping routes under a parent Router::parent("/users", [ Router::children("GET", "/all", UserController::class . "@getAllUsers"), Router::children("POST", "/add", UserController::class . "@addUser") ], [AuthMiddleware::class], ["/users/all" => "GET"]); // Initialize the router (should be called once, after all routes are defined) Router::init();
You can define dynamic segments in your route URLs using curly braces, e.g. /user/{id}
. These will be passed as parameters to your controller method.
MiddlewareInterface
.Each route registration method accepts parameters for summary, description, response codes, tags, and more, which are used to generate Swagger documentation automatically.
ResourceNotFoundException
if no route matches.ClassNotFoundException
or MethodNotFoundException
for invalid handlers.InvalidResponseTypeException
if the controller does not return a valid Response
object.Router::init()
after defining all routes.Router::parent()
and Router::children()
for better organization of related endpoints.