An open API service indexing awesome lists of open source software.

https://github.com/dannyyassine/ivory

Powered by Open Swoole: high-performance http micro PHP library
https://github.com/dannyyassine/ivory

http lib library openswoole php swoole

Last synced: about 1 year ago
JSON representation

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

Awesome Lists containing this project

README

          


logo.png



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

```php
$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:
```php
$app->get('/', HomeController::class);
```

### Group routes
```php
$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
```php
$app->get('/api/weather/city/:city', GetWeatherController::class);
```

## Use case driven controllers:
```php
class HomeController {
public function execute(Request $request): string {
return 'This is my response';
}
}
```

### Customize the response
```php
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:
```php
$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:
```php
class HomeController {
public function __construct(protected Service $service) {
//
}
}
```

## Global middlewares
```php
// before the request is handled
$app->addPreGlobalMiddleware(CheckIPMiddleware::class);

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

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