Skip to content

Lite & fast micro PHP HTTP framework that is easy to learn.

License

Notifications You must be signed in to change notification settings

utopia-php/http

Repository files navigation

Logo

Build Status Total Downloads Discord

Utopia Framework is a PHP MVC based framework with minimal must-have features for professional, simple, advanced and secure web development. This library is maintained by the Appwrite team.

Utopia Framework is dependency free. Any extra features such as authentication, caching will be available as standalone models in order to keep the framework core as clean, light and easy to learn.

Getting Started

Install using composer:

composer require utopia-php/framework

Init your first application:

require_once __DIR__ . '/../../vendor/autoload.php';

use Utopia\App;
use Utopia\Request;
use Utopia\Response;

App::get('/hello-world') // Define Route
    ->inject('request')
    ->inject('response')
    ->action(
        function($request, $response) {
            $response
              ->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
              ->addHeader('Expires', '0')
              ->addHeader('Pragma', 'no-cache')
              ->json(['Hello' => 'World']);
        }
    );

App::setMode(App::MODE_TYPE_PRODUCTION); // Define Mode

$app        = new App('America/New_York');
$request    = new Request();
$response   = new Response();

$app->run($request, $response);

Hooks

There are three types of hooks, init hooks, shutdown hooks and error hooks. Init hooks are executed before the route action is executed. Shutdown hook is executed after route action is executed before application shuts down. Finally error hooks are executed whenever there's an error in the application lifecycle. You can provide multiple hooks for each stage. If you do not assign groups to the hook, by default the hook will be executed for every route.

require_once __DIR__ . '/../../vendor/autoload.php';

use Utopia\App;
use Utopia\Request;
use Utopia\Response;

App::init()
    ->inject('response')
    ->action(function($response) {
        $response->addHeader('content-type', 'application/json');
    });

App::error()
    ->inject('error')
    ->inject('response')
    ->action(function($error, $response) {
        $response
            ->setStatusCode(500)
            ->send('Error occurred ' . $error);
    });

App::get('/hello-world') // Define Route
    ->inject('request')
    ->inject('response')
    ->action(
        function($request, $response) {
            $response
              ->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
              ->addHeader('Expires', '0')
              ->addHeader('Pragma', 'no-cache')
              ->json(['Hello' => 'World']);
        }
    );

App::setMode(App::MODE_TYPE_PRODUCTION); // Define Mode

$app        = new App('America/New_York');
$request    = new Request();
$response   = new Response();

$app->run($request, $response);

System Requirements

Utopia Framework requires PHP 8.0 or later. We recommend using the latest PHP version whenever possible.

More from Utopia

Our ecosystem support other thin PHP projects aiming to extend the core PHP Utopia framework.

Each project is focused on solving a single, very simple problem and you can use composer to include any of them in your next project.

Library Description
Utopia AB Simple PHP library for managing AB testing on the server side.
Utopia Abuse Simple PHP library for rate limiting usage of different features in your app or API.
Utopia Analytics Simple PHP library to send information about events or pageviews to Google Analytics.
Utopia Audit Simple PHP library for audit logging users actions and system events
Utopia Cache Simple PHP library for managing cache with different storage adapters.
Utopia CLI Simple PHP library for for building simple command line tools.
Utopia Config Simple PHP library for managing your app configuration.
Utopia Database</