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
+127-44
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# php-router
2
-
This router is designed to help you easily and efficiently handle HTTP requests and responses in your PHP applications. It is inspired by the popular Laravel framework, and aims to provide a similar experience and functionality.
2
+
3
+
This router is designed to help you easily and efficiently handle HTTP requests and responses in your PHP applications.
4
+
It is inspired by the popular Laravel framework, and aims to provide a similar experience and functionality.
3
5
4
6
# Getting Started
5
7
@@ -16,77 +18,151 @@ This router is designed to help you easily and efficiently handle HTTP requests
16
18
-[TODO](#todo)
17
19
18
20
## Installation
21
+
19
22
You may use composer to intall [MrF0o/php-router](https://github.com/MrF0o/php-router) by running this command:
23
+
20
24
```bash
21
25
composer require mrf0o/php-router
22
26
```
23
27
28
+
## Setup
29
+
After installing the package you may run your app either via the php command line or via a reverse-proxy server such as Apache.
30
+
31
+
#### Method 1: PHP CLI
32
+
create an `index.php` for example in the root of your project (you can name whatever you want) with this content:
33
+
34
+
```php
35
+
<?php
36
+
37
+
include_once "vendor/autoload.php";
38
+
39
+
use Mrfoo\PHPRouter\Router;
40
+
41
+
Router::get('/', function() {
42
+
echo 'Hello World!';
43
+
});
44
+
45
+
Router::run();
46
+
```
47
+
48
+
```shell
49
+
php -S localhost:8888 index.php
50
+
```
51
+
This command will run your app on port 8888. And if everything went correctly, visiting http://localhost:8888 on your browser should show the text 'Hello World'.
52
+
53
+
#### Method 2: Reverse proxy
54
+
Here you can find a .htaccess file example that you can use with the rewrite rules needed for this router to run correctly. You can use the same example code from the previous method and make sure to place the file somewhere under the document root Apache expects (Generally htdocs).
55
+
56
+
> Apache by default uses index.php as the main file so it will serve it by default. but if you want to use this router globally withing you project you want all traffic to point to the index.php even tho the user tried to access a different folder withing you project.
57
+
58
+
```apacheconf
59
+
<IfModule mod_rewrite.c>
60
+
<IfModule mod_negotiation.c>
61
+
Options -MultiViews
62
+
</IfModule>
63
+
64
+
RewriteEngine On
65
+
66
+
RewriteCond %{REQUEST_FILENAME} -d [OR]
67
+
RewriteCond %{REQUEST_FILENAME} -f
68
+
RewriteRule ^ ^$1 [N]
69
+
70
+
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
71
+
RewriteRule ^(.*)$ public/$1
72
+
73
+
RewriteCond %{REQUEST_FILENAME} !-d
74
+
RewriteCond %{REQUEST_FILENAME} !-f
75
+
RewriteRule ^ index.php
76
+
</IfModule>
77
+
```
78
+
79
+
Note: you need to change index.php to the file that contains the Router::run() call.
80
+
81
+
> [!IMPORTANT]
82
+
> This router does not support subfolders yet. You may need extra configurations in your .htaccess to make it work.
83
+
24
84
## The Router class
25
-
After the installation is complete you can start using the router by including The `Router` class. for example this route will fire whenever `/hello` is hit:
85
+
86
+
[After the installation is complete you can start using the router by including The `Router` class. for example this
87
+
route will fire whenever `/hello` is hit:]()
88
+
26
89
```php
27
90
use Mrfoo\PHPRouter\Router;
28
91
29
92
Router::get('/hello', function () {
30
-
echo '<h1>hello there</h1>';
93
+
echo '<h1>hello there</h1>';
31
94
});
32
95
33
96
Router::run();
34
97
```
35
98
36
-
You can see that each request is represented as a static method of the Router class, thus you can change the `get` with each of these methods: `post`, `put`, `patch` and `delete`.
99
+
You can see that each request is represented as a static method of the Router class, thus you can change the `get` with
100
+
each of these methods: `post`, `put`, `patch` and `delete`.
37
101
38
102
Make sure to run the Router using the `run` static method.
39
103
40
104
## Route parameters
41
-
To capture a segment in your url you can use route parameters, these parameters will be passed to the handler function in order.
105
+
106
+
To capture a segment in your url you can use route parameters, these parameters will be passed to the handler function
107
+
in order.
42
108
43
109
```php
44
110
Router::get('/hello/{name}', function ($name) {
45
-
echo '<h1>hello '.$name.'</h1>';
111
+
echo '<h1>hello '.$name.'</h1>';
46
112
})
47
113
```
48
114
49
-
> optional parameters aren't implemented yet, so this route will be matched only if the parameter {name} is present, otherwise it will give a 404 error
115
+
> optional parameters aren't implemented yet, so this route will be matched only if the parameter {name} is present,
116
+
> otherwise it will give a 404 error
50
117
51
118
## Regular Expression Constraints
52
-
Sometimes, you may need to constrain a parameter using regular expression, you can do this using the `where` method on the Route instance.
119
+
120
+
Sometimes, you may need to constrain a parameter using regular expression, you can do this using the `where` method on
121
+
the Route instance.
122
+
53
123
```php
54
124
Router::get('/user/{name}', function ($name) {
55
-
// ...
125
+
// ...
56
126
})->where('name', '[A-Za-z]+');
57
127
58
128
Router::get('/user/{id}/{name}', function (string $id, string $name) {
You may give names to your routes using the `name` method on Route instance, this will make it easier to reference your routes elsewhere in your code using the `route` helper method later.
134
+
135
+
You may give names to your routes using the `name` method on Route instance, this will make it easier to reference your
136
+
routes elsewhere in your code using the `route` helper method later.
65
137
66
138
```php
67
139
Router::get('/user/profile', function () {
68
-
// ...
140
+
// ...
69
141
})->name('profile');
70
142
```
71
143
72
144
## Grouping routes
73
-
Also, you can create route groups using the `group` method of the router class, each Route registered in the callback will share the properties passed to the `group` method.
145
+
146
+
Also, you can create route groups using the `group` method of the router class, each Route registered in the callback
147
+
will share the properties passed to the `group` method.
74
148
75
149
```php
76
150
Router::group(['prefix' => '/user'], function () {
You can generate URLs for your routes using the `route` helper method, this method accepts the name of the route and a variable count of parameters to be passed to the route.
178
+
179
+
You can generate URLs for your routes using the `route` helper method, this method accepts the name of the route and a
180
+
variable count of parameters to be passed to the route.
103
181
104
182
```php
105
183
Router::get('/user/{id}', function ($id) {
106
-
// ...
184
+
// ...
107
185
})->name('user.profile');
108
186
109
187
$url = route('user.profile', 1);
110
188
// $url = 'http://example.com/user/1'
111
189
```
112
190
113
191
# Middlewares
114
-
Middlewares are a great way to filter requests before they reach your route handler, in this router library Middlewares are represented as classes that Overrides the `handle` method in the \Mrfoo\PHPRouter\Middleware class.
192
+
193
+
Middlewares are a great way to filter requests before they reach your route handler, in this router library Middlewares
194
+
are represented as classes that Overrides the `handle` method in the \Mrfoo\PHPRouter\Middleware class.
115
195
116
196
```php
117
197
<?php
@@ -121,32 +201,33 @@ use Mrfoo\PHPRouter\Core\Middleware;
121
201
122
202
class AuthMiddleware extends Middleware
123
203
{
124
-
public function handle()
125
-
{
126
-
if (!isset($_SESSION['user_id'])) {
127
-
// redirect to login page
128
-
header('Location: /login');
129
-
exit;
130
-
}
131
-
}
204
+
public function handle()
205
+
{
206
+
if (!isset($_SESSION['user_id'])) {
207
+
// redirect to login page
208
+
header('Location: /login');
209
+
exit;
210
+
}
211
+
}
132
212
}
133
213
```
134
214
135
-
the handle method will be called before the route handler, so you can do any checks you want and redirect the user if needed.
215
+
the handle method will be called before the route handler, so you can do any checks you want and redirect the user if
216
+
needed.
136
217
137
218
Then you can use the middleware in your routes like this:
0 commit comments