Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rename library to utopia-php/http #116

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:
with:
php-version: '8.1'

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Setup Docker
run: docker-compose up -d --build

Expand All @@ -23,6 +26,6 @@ jobs:

- name: Run FPM Tests
run: docker compose exec fpm vendor/bin/phpunit --configuration phpunit.xml

- name: Run Swoole Tests
run: docker compose exec swoole vendor/bin/phpunit --configuration phpunit.xml
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ Http::init()
});
```

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

### Resources

Expand Down Expand Up @@ -254,7 +254,7 @@ Utopia Http requires PHP 8.0 or later. We recommend using the latest PHP version

Our ecosystem supports other thin PHP projects aiming to extend the core PHP Utopia Http.

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

You can find all libraries in [GitHub Utopia organization](https://github.com/utopia-php).

Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 25 additions & 25 deletions docs/Getting-Starting-Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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.

# Defining Routes
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.
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.

## Basic GET Route

Expand All @@ -17,7 +17,7 @@ use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;

$http = new Server("0.0.0.0", 8080);

Http::get('/')
->inject('request')
->inject('response')
Expand All @@ -27,7 +27,7 @@ Http::get('/')
$response->send("<div> Hello World! </div>");
}
/*
Configure your HTTP server to respond with the Utopia http.
Configure your HTTP server to respond with the Utopia http.
*/

$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) {
Expand All @@ -39,12 +39,12 @@ $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swo

$http->start();
```


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`.

## More Endpoints
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.
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.

```json
[
Expand Down Expand Up @@ -88,9 +88,9 @@ You might have noticed an additional property in the above example, i.e. `param`
All the parameters need to be defined using the `param` property which accepts the following - `$key`, `$default`, `$validator`, `$description`, `$optional` and `$injections`.

There are typically 3 types of parameters:
1. Path params ( eg: `/api/users/<userID>` )
1. Path params ( eg: `/api/users/<userID>` )
2. Query Params ( eg: `/api/users?userId=<userID>`)
3. Body Params ( These are passed in the request body in POST and PUT requests. )
3. Body Params ( These are passed in the request body in POST and PUT requests. )

Let's take a look at how these three types of params are taken care of by Utopia:

Expand All @@ -100,7 +100,7 @@ Let's take a look at how these three types of params are taken care of by Utopia

3. Body Parameters are specified using the `->param()` function as well.

Each of these params then become available to the `->action()` callback function in the same order that they were declared in.
Each of these params then become available to the `->action()` callback function in the same order that they were declared in.

### Returning a Response
Based on the type of the response you wish to return, multiple options can be used:
Expand All @@ -120,7 +120,7 @@ $response->json(['Goodbye' => 'World']);
JSON objects can be returned by passing the JSON object inside `$response->json()`.


### Setting Response Status
### Setting Response Status

You can set a status code for your response using the `setStatusCode()` function of utopia's response object.

Expand Down Expand Up @@ -149,28 +149,28 @@ use Utopia\Http\Validator\Wildcard;
$http = new Server("0.0.0.0", 8080);

Http::init(function($response) {
/*
Example of global init method. Do stuff that is common to all your endpoints in all groups.
/*
Example of global init method. Do stuff that is common to all your endpoints in all groups.
This can include things like authentication and authorisation checks, implementing rate limits and so on..
*/
}, ['response']);

Http::init(function($response) {
/*
/*
Example of init method for group1. Do stuff that is common to all your endpoints in group1.
This can include things like authentication and authorisation checks, implementing rate limits and so on..
*/
}, ['response'], 'group1');

Http::init(function($response) {
/*
Example of init method for group2. Do stuff that is common to all your endpoints in group2.
/*
Example of init method for group2. Do stuff that is common to all your endpoints in group2.
This can include things like authentication and authorisation checks, implementing rate limits and so on..
*/
}, ['response'], 'group2');

Http::shutdown(function($request) {
/*
/*
Example of global shutdown method. Do stuff that needs to be performed at the end of each request for all groups.
'*' (Wildcard validator) is optional.
This can include cleanups, logging information, recording usage stats, closing database connections and so on..
Expand All @@ -179,7 +179,7 @@ Http::shutdown(function($request) {
}, ['request'], '*');

Http::shutdown(function($request) {
/*
/*
Example of shutdown method of group1. Do stuff that needs to be performed at the end of each request for all groups.
This can include cleanups, logging information, recording usage stats, closing database connections and so on..
*/
Expand Down Expand Up @@ -211,7 +211,7 @@ Http::put('/todos/:id')
$response->json($data);
}
);

$http->start();
```

Expand All @@ -224,7 +224,7 @@ For each endpoint, you can add the following properties described below. Let’s
`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.

* #### Labels
`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.
`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.

* #### Injections
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.
Expand All @@ -238,7 +238,7 @@ Since each action in Utopia depends on certain resources, `inject` is used to ad
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.

## Init and Shutdown Methods

The Utopia http goes through the following lifecycle whenever it receives any request:

![untitled@2x](https://user-images.githubusercontent.com/43381712/146966398-0f4af03b-213e-47d7-9002-01983053c5aa.png)
Expand All @@ -248,17 +248,17 @@ In case an error occurs anywhere during the execution, the workflow executes the
The init and shutdown methods take three params:

1. Callback function
2. Array of resources required by the callback
2. Array of resources required by the callback
3. The endpoint group for which the callback is intended to run

* ### Init

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 `'*'`.
```php
Http::init(function($response) {
/*
Do stuff that is common to all your endpoints.
This can include things like authentication and authorisation checks, implementing rate limits and so on..
/*
Do stuff that is common to all your endpoints.
This can include things like authentication and authorisation checks, implementing rate limits and so on..
*/
}, ['response'], '*');
```
Expand All @@ -269,8 +269,8 @@ Utopia's shutdown callback is used to perform cleanup tasks after a request. Thi

```php
Http::shutdown(function($request) {
/*
Do stuff that needs to be performed at the end of each request.
/*
Do stuff that needs to be performed at the end of each request.
This can include cleanups, logging information, recording usage stats, closing database connections and so on..
*/

Expand Down
2 changes: 1 addition & 1 deletion example/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "utopia-php/http-app",
"require": {
"utopia-php/http": "0.33.*"
"utopia-php/http": "latest"
}
}