Skip to content

Powered by Open Swoole: high-performance http micro PHP library

License

Notifications You must be signed in to change notification settings

dannyYassine/ivory

Repository files navigation

logo.png

Powered by Open Swoole: high-performance http micro PHP library

$app = new Ivory\Application();

$app->setHost('0.0.0.0')->setPort(8000);

$app->start();

// Ivory http server listening on http://0.0.0.0:8000

Routing:

$app->get('/', HomeController::class);

Group routes

$app->group('/api', function (Router $router) {
  $router->get('/name', GetController::class);
  $router->post('/name', SaveController::class);
  $router->put('/name', UpdateController::class);
  $router->delete('/name', DeleteController::class);
});

Dynamic routes

$app->get('/api/weather/city/:city', GetWeatherController::class);

Use case driven controllers:

class HomeController {
  public function execute(Request $request): string {
    return 'This is my response';
  }
}

Customize the response

class GetUserController {
  public function execute(Request $request, Response $response): array {
    $response->header("Content-Type", "application/json");

    return [
      'user' => User::first()
    ];
  }
}

Bind services/controllers to the D.I. container:

$app->bind(Service::class, function () {
  return new Service();
});

$app->bind(HomeController::class, function (Container $c) {
  return new HomeController(
    service: $c->get(GenerateNameService::class)
  );
});

Dependencies will be injected to the constructor:

class HomeController {
  public function __construct(protected Service $service) {
    //
  }
}

Global middlewares

// before the request is handled
$app->addPreGlobalMiddleware(CheckIPMiddleware::class);

// after the request is handled
$app->addPostGlobalMiddleware(LogRequestMiddleware::class);

Controller middlewares

// third argument in router definition
$app->get('/name', NameController::class, [NameMiddleware::class]);

About

Powered by Open Swoole: high-performance http micro PHP library

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published