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
- Host: GitHub
- URL: https://github.com/dannyyassine/ivory
- Owner: dannyYassine
- License: mit
- Created: 2023-10-04T00:28:50.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-12T02:31:08.000Z (over 2 years ago)
- Last Synced: 2025-01-26T01:22:33.301Z (over 1 year ago)
- Topics: http, lib, library, openswoole, php, swoole
- Language: PHP
- Homepage:
- Size: 342 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
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]);
```