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.

Here is a basic example of how to create and use an instance of Lithe\App:

$app = new \Lithe\App;

// Global middleware
$app->use(function($req, $res, $next) {
    $next();
});

// Route for '/hello'
$app->get('/hello', function($req, $res) {
    $res->send('Hello, World!');
});

// Response for HTTP 400 error
$app->httpException(400, function($req, $res) {
    $res->send('Page Not Found');
});

// Response for HTTP 500 error
$app->httpException(500, function($req, $res, $exception) {
    $res->send('Internal Server Error');
});

// Start the application
$app->listen();

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('/api', 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');
    

httpException(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->httpException(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');
        });