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
+36-15Lines changed: 36 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,15 @@
1
-
# PHP Router
2
-
3
-
PHP Router is a simple and efficient routing library designed for PHP applications. It provides a straightforward way to define routes, handle HTTP requests, and generate URLs. Built with PSR-7 message implementation in mind, it seamlessly integrates with PHP applications.
1
+
# PHP Router
4
2
3
+
PHP Router is a simple and efficient routing library designed for PHP applications. It provides a straightforward way to
4
+
define routes, handle HTTP requests, and generate URLs. Built with PSR-7 message implementation in mind, it seamlessly
5
+
integrates with PHP applications.
5
6
6
7
## Installation
7
8
8
9
You can install PHP Router via Composer. Just run:
new \PhpDevCommunity\Route('home_page', '/', [IndexController::class]),
81
-
new \PhpDevCommunity\Route('api_articles_collection', '/api/articles', [ArticleController::class, 'getAll']),
82
-
new \PhpDevCommunity\Route('api_articles', '/api/articles/{id}', [ArticleController::class, 'get']),
83
-
];
85
+
86
+
if (PHP_VERSION_ID >= 80000) {
87
+
$attributeRouteCollector = new AttributeRouteCollector([
88
+
IndexController::class,
89
+
ArticleController::class
90
+
]);
91
+
$routes = $attributeRouteCollector->collect();
92
+
}else {
93
+
$routes = [
94
+
new \PhpDevCommunity\Route('home_page', '/', [IndexController::class]),
95
+
new \PhpDevCommunity\Route('api_articles_collection', '/api/articles', [ArticleController::class, 'getAll']),
96
+
new \PhpDevCommunity\Route('api_articles', '/api/articles/{id}', [ArticleController::class, 'get']),
97
+
];
98
+
}
84
99
85
100
// Initialize the router
86
101
$router = new \PhpDevCommunity\Router($routes, 'http://localhost');
@@ -120,15 +135,18 @@ try {
120
135
121
136
## Route Definition
122
137
123
-
Routes can be defined using the `Route` class provided by PHP Router. You can specify HTTP methods, attribute constraints, and handler methods for each route.
138
+
Routes can be defined using the `Route` class provided by PHP Router. You can specify HTTP methods, attribute
139
+
constraints, and handler methods for each route.
124
140
125
141
```php
126
142
$route = new \PhpDevCommunity\Route('api_articles_post', '/api/articles', [ArticleController::class, 'post'], ['POST']);
127
143
$route = new \PhpDevCommunity\Route('api_articles_put', '/api/articles/{id}', [ArticleController::class, 'put'], ['PUT']);
128
144
```
145
+
129
146
### Easier Route Definition with Static Methods
130
147
131
-
To make route definition even simpler and more intuitive, the `RouteTrait` provides static methods for creating different types of HTTP routes. Here's how to use them:
148
+
To make route definition even simpler and more intuitive, the `RouteTrait` provides static methods for creating
149
+
different types of HTTP routes. Here's how to use them:
The `Route` object allows you to define constraints on route parameters using the `where` methods. These constraints validate and filter parameter values based on regular expressions. Here's how to use them:
245
+
The `Route` object allows you to define constraints on route parameters using the `where` methods. These constraints
246
+
validate and filter parameter values based on regular expressions. Here's how to use them:
228
247
229
248
#### Method `whereNumber()`
230
249
@@ -535,7 +554,8 @@ Example Usage:
535
554
$route = (new Route('product', '/product/{code}'))->where('code', '\d{4}');
536
555
```
537
556
538
-
By using these `where` methods, you can apply precise constraints on your route parameters, ensuring proper validation of input values.
557
+
By using these `where` methods, you can apply precise constraints on your route parameters, ensuring proper validation
0 commit comments