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
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.
17
17
18
18
### 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
-
21
19
```
22
20
<IfModule mod_rewrite.c>
23
21
<IfModule mod_negotiation.c>
@@ -36,16 +34,14 @@ If you are using Apache HTTP server, you must have a file named `.htaccess` in y
36
34
```
37
35
38
36
### NGINX
39
-
If you are using NGINX web server, you should consider following directive in your site configuration file.
40
-
41
37
```nginx
42
38
location / {
43
39
try_files $uri $uri/ /index.php?$query_string;
44
40
}
45
41
```
46
42
47
43
## 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:
49
45
50
46
```php
51
47
use MiladRahimi\PhpRouter\Router;
@@ -59,21 +55,17 @@ $router->get('/', function () {
59
55
});
60
56
61
57
$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"]);
67
59
});
68
60
69
61
$router->dispatch();
70
62
```
71
63
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).
73
65
74
66
## HTTP Methods
75
67
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:
77
69
78
70
```php
79
71
use MiladRahimi\PhpRouter\Router;
@@ -96,6 +88,9 @@ $router
96
88
->delete('/', function () {
97
89
return '<b>DELETE method</b>';
98
90
})
91
+
->any('/page', function () {
92
+
return 'This is the Page! No matter what the HTTP method is!';
93
+
})
99
94
->dispatch();
100
95
```
101
96
@@ -119,20 +114,6 @@ $router
119
114
->dispatch();
120
115
```
121
116
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
-
136
117
## Controllers
137
118
138
119
PhpRouter supports plenty of controller types, just look at following examples.
@@ -240,7 +221,7 @@ $router->get('/path/to/{info?}', function ($info = 'Default') {
240
221
$router->dispatch();
241
222
```
242
223
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.
244
225
245
226
```php
246
227
use MiladRahimi\PhpRouter\Router;
@@ -281,12 +262,12 @@ $router = new Router();
281
262
282
263
$router->get('/', function (ServerRequest $request) {
0 commit comments