Skip to content

Commit

Permalink
Merge branch 'master' of github.com:miladrahimi/phprouter
Browse files Browse the repository at this point in the history
  • Loading branch information
miladrahimi committed Dec 7, 2019
2 parents debdff8 + 9ace566 commit 00bc329
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
21 changes: 6 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Supported features:

* **v4.x.x (LTS)**
* v3.x.x (Unsupported)

The versions *v2* and *v1* are not available in this repository, please consider an upgrade to the newer versions if you are still using them.
* v2.x.x (Unavailable)
* v1.x.x (Unavailable)

## Installation

Expand Down Expand Up @@ -70,26 +70,17 @@ After the configurations mentioned above, you can start using PhpRouter in your
```php
use MiladRahimi\PhpRouter\Router;
use Zend\Diactoros\Response\JsonResponse;
$router = new Router();
$router->get('/', function () {
return '<p>This is homepage!</p>';
});
$router->post('/blog/post/{id}', function ($id) {
return HtmlResponse("<p>This is a post $id</p>");
});
$router->patch('/json', function () {
return new JsonResponse(["message" => "posted data to user: $id"]);
});
$router->dispatch();
```

There are more examples [here](https://github.com/miladrahimi/phprouter/blob/master/examples/index.php).
There are more examples [here](https://github.com/miladrahimi/phprouter/tree/master/examples).

## HTTP Methods

Expand Down Expand Up @@ -266,7 +257,7 @@ $router->dispatch();

## HTTP Request and Request

PhpRouter uses [zend-diactoros](https://github.com/zendframework/zend-diactoros) package (v2) to provide [PSR-7](https://www.php-fig.org/psr/psr-7) complaint request and response objects to your controllers and middleware.
PhpRouter uses [zend-diactoros](https://github.com/zendframework/zend-diactoros) package (v2) to provide [PSR-7](https://www.php-fig.org/psr/psr-7) request and response objects to your controllers and middleware.

### Request

Expand Down Expand Up @@ -368,9 +359,9 @@ interface Middleware
}
```

As you can see, a middleware must have a `handle()` method that catches the request and a Closure (which is responsible for running the next middleware or the controller). It must return a response, as well. A middleware can break the lifecycle and return the response or it can run the `$next` closure to continue the lifecycle.
As you can see, a middleware must have a `handle()` method that catches the request and a Closure (which is responsible for running the next middleware or the controller). It must return a response, as well. A middleware can break the lifecycle and return a response or it can run the `$next` closure to continue the lifecycle.

See the following example. In this snippet, if there is an `Authorization` header in the request, it passes the request to the next middleware or the controller (if there is no more middleware left) and if the header is absent, it returns an empty response with `401 Authorization Failed ` HTTP status code.
See the following example. In the implemented middelware, if there is an `Authorization` header in the request, it passes the request to the next middleware or the controller (if there is no more middleware left) and if the header is absent, it returns a JSON response with `401 Authorization Failed ` HTTP status code.

```php
use MiladRahimi\PhpRouter\Router;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "miladrahimi/phprouter",
"description": "PhpRouter is a powerful and standalone HTTP URL router for PHP projects.",
"description": "PhpRouter is a powerful, standalone, and very fast HTTP URL router for PHP projects.",
"keywords": [
"Router",
"URL Router",
Expand Down
5 changes: 0 additions & 5 deletions examples/example-01/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
require('../../vendor/autoload.php');

use MiladRahimi\PhpRouter\Router;
use Zend\Diactoros\Response\JsonResponse;

$router = new Router();

$router->get('/', function () {
return '<p>This is homepage!</p>';
});

$router->post('/api/user/{id}', function ($id) {
return new JsonResponse(["message" => "posted data to user: $id"]);
});

$router->dispatch();

0 comments on commit 00bc329

Please sign in to comment.