Skip to content

Commit c4b9303

Browse files
Merge pull request #140 from utopia-php/feat-updating-readme
feat: updating readme
2 parents fc63ec6 + 5669f39 commit c4b9303

File tree

3 files changed

+68
-19
lines changed

3 files changed

+68
-19
lines changed

README.md

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Utopia HTTP 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](https://appwrite.io).
1010

11-
Utopia HTTP is dependency-free. Any extra features, such as authentication or caching are available as standalone models in order to keep the framework core clean, light, and easy to learn.
11+
Utopia HTTP is *almost* dependency-free. Any extra features, such as authentication or caching are available as standalone models in order to keep the framework core clean, light, and easy to learn.
1212

1313
## Getting Started
1414

@@ -20,30 +20,47 @@ composer require utopia-php/http
2020

2121
Init your first application in `src/server.php`:
2222

23+
2324
```php
2425
require_once __DIR__.'/../vendor/autoload.php';
2526

27+
use Utopia\DI\Container;
28+
use Utopia\DI\Dependency;
2629
use Utopia\Http\Http;
2730
use Utopia\Http\Request;
2831
use Utopia\Http\Response;
2932
use Utopia\Http\Adapter\FPM\Server;
3033

31-
Http::get('/hello-world') // Define Route
32-
->inject('request')
33-
->inject('response')
34+
// Creating the dependency injection container
35+
$container = new Container();
36+
37+
// Adding a user dependency to the container
38+
$user = new Dependency();
39+
$user
40+
->setName('user')
41+
->inject('request') // We can insert and use other injections as well
42+
->setCallback(fn (Request $request) => $request->getHeader('x-user-id', 'John Doe'));
43+
44+
$container->add($user);
45+
46+
// Defining Route
47+
Http::get('/hello-world')
48+
->inject('request') // Auto-injected each request
49+
->inject('response') // Auto-injected each request
50+
->inject('user')
3451
->action(
35-
function(Request $request, Response $response) {
52+
function(Request $request, Response $response, string $user) {
3653
$response
3754
->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
3855
->addHeader('Expires', '0')
3956
->addHeader('Pragma', 'no-cache')
40-
->json(['Hello' => 'World']);
57+
->json(['message' => 'Hello World', 'user' => $user]);
4158
}
4259
);
4360

4461
Http::setMode(Http::MODE_TYPE_PRODUCTION);
4562

46-
$http = new Http(new Server(), 'America/New_York');
63+
$http = new Http(new Server(), $container, 'America/New_York');
4764
$http->start();
4865
```
4966

@@ -66,6 +83,7 @@ The library supports server adapters to be able to run on any PHP setup. You cou
6683
#### Use PHP FPM server
6784

6885
```php
86+
use Utopia\DI\Container;
6987
use Utopia\Http\Http;
7088
use Utopia\Http\Response;
7189
use Utopia\Http\Adapter\FPM\Server;
@@ -78,7 +96,7 @@ Http::get('/')
7896
}
7997
);
8098

81-
$http = new Http(new Server(), 'America/New_York');
99+
$http = new Http(new Server(), new Container() 'America/New_York');
82100
$http->start();
83101
```
84102

@@ -87,6 +105,7 @@ $http->start();
87105
#### Using Swoole server
88106

89107
```php
108+
use Utopia\DI\Container;
90109
use Utopia\Http\Http;
91110
use Utopia\Http\Request;
92111
use Utopia\Http\Response;
@@ -101,13 +120,13 @@ Http::get('/')
101120
}
102121
);
103122

104-
$http = new Http(new Server('0.0.0.0', '80'), 'America/New_York');
123+
$http = new Http(new Server('0.0.0.0', '80' , ['open_http2_protocol' => true]), new Container(), 'America/New_York');
105124
$http->start();
106125
```
107126

108127
> When using Swoole, you can use the command `php src/server.php` to run the HTTP server locally, but you need Swoole installed. For setup with Docker, check out our [example application](/example)
109128
110-
### Parameters
129+
### Parameters
111130

112131
Parameters are used to receive input into endpoint action from the HTTP request. Parameters could be defined as URL parameters or in a body with a structure such as JSON.
113132

@@ -206,16 +225,24 @@ Http::init()
206225

207226
Groups are designed to be actions that run during the lifecycle of requests to endpoints that have some logic in common. Groups allow you to prevent code duplication and are designed to be defined anywhere in your source code to allow flexibility.
208227

209-
### Resources
228+
### Injections
210229

211-
Resources allow you to prepare dependencies for requests such as database connection or the user who sent the request. A new instance of a resource is created for every request.
230+
Injections allow you to prepare dependencies for requests such as database connection or the user who sent the request. A new instance of a resource is created for every request.
212231

213-
Define a resource:
232+
We define an injection using a Container:
214233

215234
```php
216-
Http::setResource('timing', function() {
217-
return \microtime(true);
218-
});
235+
use Utopia\DI\Container;
236+
use Utopia\DI\Dependency;
237+
238+
$container = new Container();
239+
240+
$timing = new Dependency();
241+
$timing
242+
->setName('timing')
243+
->setCallback(fn () => \microtime(true));
244+
245+
$container->add($timing);
219246
```
220247

221248
Inject resource into endpoint action:

example/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "utopia-php/http-app",
33
"require": {
4-
"utopia-php/http": "latest"
4+
"utopia-php/http": "1.0.*"
55
}
6-
}
6+
}

example/src/server.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,39 @@
22

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

5+
use Utopia\DI\Container;
6+
use Utopia\DI\Dependency;
57
use Utopia\Http\Http;
68
use Utopia\Http\Response;
79
use Utopia\Http\Adapter\Swoole\Server;
810
use Utopia\Http\Validator\Text;
911

12+
class User
13+
{
14+
public function __construct(public $name)
15+
{
16+
}
17+
}
18+
19+
$container = new Container();
20+
21+
$user = new Dependency();
22+
$user->setName('user')->setCallback(fn () => new User("Demo user"));
23+
$container->set($user);
24+
1025
Http::get('/')
1126
->param('name', 'World', new Text(256), 'Name to greet. Optional, max length 256.', true)
1227
->inject('response')
1328
->action(function (string $name, Response $response) {
1429
$response->send('Hello ' . $name);
1530
});
1631

17-
$http = new Http(new Server('0.0.0.0', '80'), 'America/New_York');
32+
Http::get('/user')
33+
->inject('response')
34+
->inject('user')
35+
->action(function (Response $response, User $user) {
36+
$response->send('Hello ' . $user->name);
37+
});
38+
39+
$http = new Http(new Server('0.0.0.0', '80'), $container, 'America/New_York');
1840
$http->start();

0 commit comments

Comments
 (0)