You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -204,7 +204,7 @@ Http::init()
204
204
});
205
205
```
206
206
207
-
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.
207
+
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.
208
208
209
209
### Resources
210
210
@@ -254,7 +254,7 @@ Utopia Http requires PHP 8.0 or later. We recommend using the latest PHP version
254
254
255
255
Our ecosystem supports other thin PHP projects aiming to extend the core PHP Utopia Http.
256
256
257
-
Each project is focused on solving a single, very simple problem and you can use composer to include any of them in your next project.
257
+
Each project is focused on solving a single, very simple problem and you can use composer to include any of them in your next project.
258
258
259
259
You can find all libraries in [GitHub Utopia organization](https://github.com/utopia-php).
Copy file name to clipboardExpand all lines: docs/Getting-Starting-Guide.md
+25-25Lines changed: 25 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
Utopia Http is an easy-to-use PHP MVC based framework with minimal must-have features for professional, simple, advanced and secure web development. It follows an architecture like Express and is based on the declarative programming approach. Documenting and writing code are usually seen as two separate tasks and very often, documentation loses priority in the software development lifecycle. Utopia unifies the two with a flexible API that allows your code to be self-documenting. What’s interesting about Utopia is its ability to accept metadata along with it’s route definitions. This metadata can then be used for various purposes like generating documentation, swagger specifications and more.
5
5
6
6
# Defining Routes
7
-
If you’re new to Utopia, let’s get started by looking at an example of a basic GET route for an application that you can create using Utopia. We'll be using a [Swoole server](https://github.com/swoole/swoole-src) in this example, but you should be able to extend it to any HTTP server.
7
+
If you’re new to Utopia, let’s get started by looking at an example of a basic GET route for an application that you can create using Utopia. We'll be using a [Swoole server](https://github.com/swoole/swoole-src) in this example, but you should be able to extend it to any HTTP server.
8
8
9
9
## Basic GET Route
10
10
@@ -17,7 +17,7 @@ use Swoole\Http\Request as SwooleRequest;
17
17
use Swoole\Http\Response as SwooleResponse;
18
18
19
19
$http = new Server("0.0.0.0", 8080);
20
-
20
+
21
21
Http::get('/')
22
22
->inject('request')
23
23
->inject('response')
@@ -27,7 +27,7 @@ Http::get('/')
27
27
$response->send("<div> Hello World! </div>");
28
28
}
29
29
/*
30
-
Configure your HTTP server to respond with the Utopia http.
30
+
Configure your HTTP server to respond with the Utopia http.
31
31
*/
32
32
33
33
$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) {
@@ -39,12 +39,12 @@ $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swo
39
39
40
40
$http->start();
41
41
```
42
-
42
+
43
43
44
44
Any route in Utopia would require you to `inject` the dependencies ( `$request` and `$response` in this case ) and define the controller by passing a callback to the `action` function. As you might have already guessed, `$request` and `$response` refer to the objects of the HTTP server library you’re using, for example, Swoole in this case. `action` defines the callback function that would be called when the GET request is executed. In this case, raw HTML is returned as a `$response`.
45
45
46
46
## More Endpoints
47
-
You can perform basic CRUD operations like GET, POST, PUT and DELETE using Utopia. Let’s assume there's a file `todos.json` that stores a list of todo objects with the following structure. In a real-world scenario, you would be fetching this information from a database.
47
+
You can perform basic CRUD operations like GET, POST, PUT and DELETE using Utopia. Let’s assume there's a file `todos.json` that stores a list of todo objects with the following structure. In a real-world scenario, you would be fetching this information from a database.
48
48
49
49
```json
50
50
[
@@ -88,9 +88,9 @@ You might have noticed an additional property in the above example, i.e. `param`
88
88
All the parameters need to be defined using the `param` property which accepts the following - `$key`, `$default`, `$validator`, `$description`, `$optional` and `$injections`.
Example of shutdown method of group1. Do stuff that needs to be performed at the end of each request for all groups.
184
184
This can include cleanups, logging information, recording usage stats, closing database connections and so on..
185
185
*/
@@ -211,7 +211,7 @@ Http::put('/todos/:id')
211
211
$response->json($data);
212
212
}
213
213
);
214
-
214
+
215
215
$http->start();
216
216
```
217
217
@@ -224,7 +224,7 @@ For each endpoint, you can add the following properties described below. Let’s
224
224
`groups` are used to define common functions that need to be executed. When you add a callback function to a group, the init hooks of the respective group are executed before the individual actions are executed.
225
225
226
226
*#### Labels
227
-
`label` can be used to store metadata that is related to your endpoint. It’s a key-value store. Some use-cases can be using label to generate the documentation or the swagger specifications.
227
+
`label` can be used to store metadata that is related to your endpoint. It’s a key-value store. Some use-cases can be using label to generate the documentation or the swagger specifications.
228
228
229
229
*#### Injections
230
230
Since each action in Utopia depends on certain resources, `inject` is used to add the dependencies. `$response` and `$request` can be injected into the service. Utopia provides the http static functions to make global resources available to all utopia endpoints.
@@ -238,7 +238,7 @@ Since each action in Utopia depends on certain resources, `inject` is used to ad
238
238
Now that you’re familiar with routing in Utopia, let’s dive into the lifecycle of a utopia request in detail and learn about some of the lifecycle methods.
239
239
240
240
## Init and Shutdown Methods
241
-
241
+
242
242
The Utopia http goes through the following lifecycle whenever it receives any request:
@@ -248,17 +248,17 @@ In case an error occurs anywhere during the execution, the workflow executes the
248
248
The init and shutdown methods take three params:
249
249
250
250
1. Callback function
251
-
2. Array of resources required by the callback
251
+
2. Array of resources required by the callback
252
252
3. The endpoint group for which the callback is intended to run
253
253
254
254
*### Init
255
255
256
256
init method is executed in the beginning when the program execution begins. Here’s an example of the init method, where the init method is executed for all groups indicated by the wildcard symbol `'*'`.
257
257
```php
258
258
Http::init(function($response) {
259
-
/*
260
-
Do stuff that is common to all your endpoints.
261
-
This can include things like authentication and authorisation checks, implementing rate limits and so on..
259
+
/*
260
+
Do stuff that is common to all your endpoints.
261
+
This can include things like authentication and authorisation checks, implementing rate limits and so on..
262
262
*/
263
263
}, ['response'], '*');
264
264
```
@@ -269,8 +269,8 @@ Utopia's shutdown callback is used to perform cleanup tasks after a request. Thi
269
269
270
270
```php
271
271
Http::shutdown(function($request) {
272
-
/*
273
-
Do stuff that needs to be performed at the end of each request.
272
+
/*
273
+
Do stuff that needs to be performed at the end of each request.
274
274
This can include cleanups, logging information, recording usage stats, closing database connections and so on..
0 commit comments