Lithe\App
The `Lithe\App` class is the main entry point for applications in Lithe. It manages the application's lifecycle, middleware configurations, routes, and responds to HTTP requests.
Description
The Lithe\App
class is the foundation for creating and managing an application in Lithe. It provides methods to configure middleware, define routes, handle HTTP exceptions, and start the application. Through App
, you can manage application configuration, control execution flow, and provide responses to incoming HTTP requests.
Methods
METHOD(string $path, callable|array ...$handler): void
- Description: Defines a route for a specific HTTP method.
- Parameters:
$path
- The route path.$handler
- Handlers (callbacks) for the route.
- Available Methods: GET, POST, PUT, DELETE, HEAD, PATCH
$app->get('/welcome', function($req, $res) { $res->send('Welcome!'); }); $app->post('/submit', function($req, $res) { $res->send('Form Submitted'); });
use(string|callable|Router|array ...$middleware): void
- Description: Adds middleware to the application. It can be a callback function, a string for a middleware path, a
Router
object, or an array of middlewares. - Parameters:
$middleware
- Middleware or router to be added.
$app->use(function($req, $res, $next) { // Middleware logic $next(); });
set(string $key, mixed $value): void
- Description: Sets a global configuration for the application.
- Parameters:
$key
- Configuration key.$value
- Configuration value.
$app->set('view engine', 'blade');
fail(int $status, callable $handler): void
- Description: Defines a specific handler for HTTP exceptions based on the status code.
- Parameters:
$status
- HTTP status code for which the handler will be defined.$handler
- Handler to manage the HTTP exception.
$app->fail(404, function($req, $res) { $res->status(404)->send('Page Not Found'); });
listen(): void
- Description: Allows the application to listen to and respond to HTTP requests.
$app->listen();
route(string $path): object
- Description: Creates an object to define routes with a specific prefix.
- Parameters:
$path
- Route prefix.
- Return: An anonymous object to define routes with the given prefix.
$app->route('/api') ->get(function($req, $res) { $res->send('User List'); });