Index
## Route class The `Route` class in the MMO framework is responsible for managing the current request, executing middleware, and handling the response. Here's a breakdown of its key functionalities: ### Example Usage In this example, a `Route` object is created, a request is set, middleware is added, and the request is handled to generate a response. The response is then sent to the client. ```php use MMO\core\server\Route; use MMO\core\server\Request; use MMO\core\server\Response; $request = Request::Server(); $route = new Route('origin'); $route->setRequest($request); // Add middleware $route->addMiddleware(function($route) { // Add your Middleware logic return $route->getResponse() ?: new Response(); }); // Handle the request and get the response $response = $route->handle(); // Send the response $response->send(); ``` 1. **Request Handling**: - The `ServerRoute` class holds the current request (`$request`) and response (`$response`). - It provides methods to get and set the request and response, as well as to extract data from the request (e.g., query parameters, headers). 2. **Middleware Management**: - Middlewares are functions that process the request and response. They can be added to the route using the `addMiddleware` method. - The middlewares are stored in an array and can be sorted by their order of execution. - The `handle` method executes the middlewares in sequence and processes the request. 3. **Error Handling**: - The `ServerRoute` class allows setting an error handler to manage exceptions that occur during request processing. - The `setErrorHandler` method sets a custom error handler function. 4. **Response Handling**: - The class provides methods to manipulate the response, such as setting the response body, content type, and headers. - The `handle` method ensures that a response is generated and returned after processing the request. ## Application router The `Application` class is the main module that routes requests and sends responses to the server. It connects routes and manages the request lifecycle. ### Defining Routes Routes are connected using the `connectRoutes` method of the `Application` class. ### Example ```php use MMO\core\Application; use MMO\core\attributes\APIRoute; use MMO\core\server\Route; use MMO\core\interfaces\iResponse; class MyController { #[APIRoute('GET|data/{repository}/{collection}.{format}', 'Get data from the repository', mediaTypes: ['json', 'csv'])] public function getData(Route $route): iResponse { // Route handling logic } } // Create an instance of the application $application = Application::Shared(); // Connect routes $application->connectRoutes(MyController::class); // Run the application $application->run(); ``` ### Handling Requests The `Application` class handles requests by creating a `Route` object, dispatching it, executing middleware, and sending the response. ### Example ```php use MMO\core\Application; use MMO\core\server\Request; $application = Application::Shared(); $request = Request::Server(); // Handle the request and get the response $response = $application->handle($request); // Send the response $application->sendResponse($response); ```