Skip to content

Commit eb503cd

Browse files
committed
feat: updating readme
1 parent cc880ec commit eb503cd

File tree

3 files changed

+73
-19
lines changed

3 files changed

+73
-19
lines changed

README.md

Lines changed: 50 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,54 @@ 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+
class User {
35+
public string $name;
36+
37+
public function __construct(string $name)
38+
{
39+
$this->name = $name;
40+
}
41+
}
42+
// Creating the dependency injection container
43+
$container = new Container();
44+
45+
// Adding a user dependency to the container
46+
$user = new Dependency();
47+
$user
48+
->setName('user')
49+
->setCallback(fn () => new User('John Doe'));
50+
51+
$container->add($user);
52+
53+
// Defining Route
54+
Http::get('/hello-world')
55+
->inject('request') // Auto-injected each request
56+
->inject('response') // Auto-injected each request
57+
->inject('user')
3458
->action(
35-
function(Request $request, Response $response) {
59+
function(Request $request, Response $response, User $user) {
3660
$response
3761
->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
3862
->addHeader('Expires', '0')
3963
->addHeader('Pragma', 'no-cache')
40-
->json(['Hello' => 'World']);
64+
->json(['Hello' => 'World', 'User is' => $user->name]);
4165
}
4266
);
4367

4468
Http::setMode(Http::MODE_TYPE_PRODUCTION);
4569

46-
$http = new Http(new Server(), 'America/New_York');
70+
$http = new Http(new Server(), $container, 'America/New_York');
4771
$http->start();
4872
```
4973

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

6892
```php
93+
use Utopia\DI\Container;
6994
use Utopia\Http\Http;
7095
use Utopia\Http\Response;
7196
use Utopia\Http\Adapter\FPM\Server;
@@ -78,7 +103,7 @@ Http::get('/')
78103
}
79104
);
80105

81-
$http = new Http(new Server(), 'America/New_York');
106+
$http = new Http(new Server(), new Container() 'America/New_York');
82107
$http->start();
83108
```
84109

@@ -87,6 +112,7 @@ $http->start();
87112
#### Using Swoole server
88113

89114
```php
115+
use Utopia\DI\Container;
90116
use Utopia\Http\Http;
91117
use Utopia\Http\Request;
92118
use Utopia\Http\Response;
@@ -101,13 +127,13 @@ Http::get('/')
101127
}
102128
);
103129

104-
$http = new Http(new Server('0.0.0.0', '80'), 'America/New_York');
130+
$http = new Http(new Server('0.0.0.0', '80' , ['open_http2_protocol' => true]), new Container(), 'America/New_York');
105131
$http->start();
106132
```
107133

108134
> 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)
109135
110-
### Parameters
136+
### Parameters
111137

112138
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.
113139

@@ -206,16 +232,24 @@ Http::init()
206232

207233
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.
208234

209-
### Resources
235+
### Injections
210236

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.
237+
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.
212238

213-
Define a resource:
239+
We define an injection using a Container:
214240

215241
```php
216-
Http::setResource('timing', function() {
217-
return \microtime(true);
218-
});
242+
use Utopia\DI\Container;
243+
use Utopia\DI\Dependency;
244+
245+
$container = new Container();
246+
247+
$timing = new Dependency();
248+
$timing
249+
->setName('timing')
250+
->setCallback(fn () => \microtime(true));
251+
252+
$container->add($timing);
219253
```
220254

221255
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: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,37 @@
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+
13+
class User {
14+
function __construct(public $name) {}
15+
}
16+
17+
$container = new Container();
18+
19+
$user = new Dependency();
20+
$user->setName('user')->setCallback(fn() => new User("Demo user"));
21+
$container->set($user);
22+
1023
Http::get('/')
1124
->param('name', 'World', new Text(256), 'Name to greet. Optional, max length 256.', true)
1225
->inject('response')
1326
->action(function (string $name, Response $response) {
1427
$response->send('Hello ' . $name);
1528
});
1629

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

0 commit comments

Comments
 (0)