Skip to content

Commit 4964fd2

Browse files
author
Milad Rahimi
authored
Update README.md
1 parent 9484426 commit 4964fd2

File tree

1 file changed

+17
-51
lines changed

1 file changed

+17
-51
lines changed

README.md

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ composer require miladrahimi/phprouter "4.*"
1313
```
1414

1515
## Configuration
16-
First of all, you need to configure your web server to handle all the HTTP requests with a single PHP file like `index.php`. Here you can see required configurations for Apache HTTP Server and NGINX.
16+
First of all, you need to configure your web server to handle all the HTTP requests with a single PHP file like `index.php`. Here you can see sample configurations for Apache HTTP Server and NGINX.
1717

1818
### Apache
19-
If you are using Apache HTTP server, you must have a file named `.htaccess` in your project's root directory contains following content.
20-
2119
```
2220
<IfModule mod_rewrite.c>
2321
<IfModule mod_negotiation.c>
@@ -36,16 +34,14 @@ If you are using Apache HTTP server, you must have a file named `.htaccess` in y
3634
```
3735

3836
### NGINX
39-
If you are using NGINX web server, you should consider following directive in your site configuration file.
40-
4137
```nginx
4238
location / {
4339
try_files $uri $uri/ /index.php?$query_string;
4440
}
4541
```
4642

4743
## Getting Started
48-
After configurations above, you can start using PhpRouter in your entry point (`index.php`) like this example:
44+
After configurations above, you can start using PhpRouter in your entry point file (`index.php`) like this example:
4945

5046
```php
5147
use MiladRahimi\PhpRouter\Router;
@@ -59,21 +55,17 @@ $router->get('/', function () {
5955
});
6056

6157
$router->post('/blog/post/{id}', function ($id) {
62-
return HtmlResponse("<p>This is a post $id</p>");
63-
});
64-
65-
$router->patch('/json', function () {
66-
return JsonResponse(['message' => 'This is a JSON response!']);
58+
return JsonResponse(["message" => "This is a post $id"]);
6759
});
6860

6961
$router->dispatch();
7062
```
7163

72-
There are also some examples [here](https://github.com/miladrahimi/phprouter/blob/master/examples/index.php).
64+
There are more examples [here](https://github.com/miladrahimi/phprouter/blob/master/examples/index.php).
7365

7466
## HTTP Methods
7567

76-
Here you can see how to declare different routes with different http methods:
68+
Here you can see how to declare different routes for different http methods:
7769

7870
```php
7971
use MiladRahimi\PhpRouter\Router;
@@ -96,6 +88,9 @@ $router
9688
->delete('/', function () {
9789
return '<b>DELETE method</b>';
9890
})
91+
->any('/page', function () {
92+
return 'This is the Page! No matter what the HTTP method is!';
93+
})
9994
->dispatch();
10095
```
10196

@@ -119,20 +114,6 @@ $router
119114
->dispatch();
120115
```
121116

122-
You also may want to respond to all the http methods so this one is for you:
123-
124-
```php
125-
use MiladRahimi\PhpRouter\Router;
126-
127-
$router = new Router();
128-
129-
$router->any('/', function () {
130-
return 'This is Home! No matter what the HTTP method is!';
131-
});
132-
133-
$router->dispatch();
134-
```
135-
136117
## Controllers
137118

138119
PhpRouter supports plenty of controller types, just look at following examples.
@@ -240,7 +221,7 @@ $router->get('/path/to/{info?}', function ($info = 'Default') {
240221
$router->dispatch();
241222
```
242223

243-
In default, route parameters can match any value, but you can define a regular expression for them and it applys to all of them in all the routes.
224+
In default, route parameters can match any value, but you can define regex pattern for them.
244225

245226
```php
246227
use MiladRahimi\PhpRouter\Router;
@@ -281,12 +262,12 @@ $router = new Router();
281262

282263
$router->get('/', function (ServerRequest $request) {
283264
return new JsonResponse([
284-
'method' => $request->getMethod(),
285-
'uri' => $request->getUri(),
265+
'method' => $request->getMethod(), // "GET"
266+
'uri' => $request->getUri(), // "/"
286267
'body' => $request->getBody(),
287268
'parsedBody' => $request->getParsedBody(),
288269
'headers' => $request->getHeaders(),
289-
'queryParameters' => $request->getQueryParams(),
270+
'queryParameters' => $request->getQueryParams(), // Query strings
290271
'attributes' => $request->getAttributes(),
291272
]);
292273
});
@@ -297,7 +278,7 @@ $router->post('/blog/posts', function (ServerRequest $request) {
297278
$post->content = $request->getQueryParams()['content'];
298279
$post->save();
299280

300-
return new EmptyResponse(201);
281+
return new EmptyResponse(204);
301282
});
302283

303284
$router->dispatch();
@@ -330,28 +311,13 @@ $router
330311
return new TextResponse('This is a plain text...');
331312
})
332313
->get('/empty', function () {
333-
return new EmptyResponse();
314+
return new EmptyResponse(); // HTTP Status: 204
315+
})
316+
->get('/redirect', function () {
317+
return new RedirectResponse('https://miladrahimi.com');
334318
});
335319

336320
$router->dispatch();
337-
338-
```
339-
340-
#### Redirection Response
341-
342-
In case of needing to redirecting user to another URL:
343-
344-
```php
345-
use MiladRahimi\PhpRouter\Router;
346-
use Zend\Diactoros\Response\RedirectResponse;
347-
348-
$router = new Router();
349-
350-
$router
351-
->get('/redirect', function () {
352-
return new RedirectResponse('https://miladrahimi.com');
353-
})
354-
->dispatch();
355321
```
356322

357323
### More about HTTP Request and Response

0 commit comments

Comments
 (0)