Skip to content

Commit bfd2650

Browse files
committed
HTTP methods have been updated to process requests dynamically, README.md has been updated.
1 parent 006d391 commit bfd2650

File tree

3 files changed

+152
-76
lines changed

3 files changed

+152
-76
lines changed

README.md

Lines changed: 145 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@
66
composer require lion-framework/lion-route
77
```
88

9+
## HTACCESS
10+
```apacheconf
11+
<IfModule mod_rewrite.c>
12+
<IfModule mod_negotiation.c>
13+
Options -MultiViews -Indexes
14+
</IfModule>
15+
16+
RewriteEngine On
17+
18+
# Handle Authorization Header
19+
RewriteCond %{HTTP:Authorization} .
20+
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
21+
22+
# Redirect Trailing Slashes If Not A Folder...
23+
RewriteCond %{REQUEST_FILENAME} !-d
24+
RewriteCond %{REQUEST_URI} (.+)/$
25+
RewriteRule ^ %1 [L,R=301]
26+
27+
# Send Requests To Front Controller...
28+
RewriteCond %{REQUEST_FILENAME} !-d
29+
RewriteCond %{REQUEST_FILENAME} !-f
30+
RewriteRule ^ index.php [L]
31+
</IfModule>
32+
```
33+
934
## Usage
1035
```php
1136
require_once("vendor/autoload.php");
@@ -22,7 +47,7 @@ Route::any('/', function() {
2247
});
2348

2449
// 1 is for production and 2+ for local environment.
25-
Route::processOutput(Route::dispatch(2));
50+
Route::dispatch(2);
2651
```
2752

2853
### Defining routes:
@@ -53,60 +78,11 @@ use in routes:
5378
'/user/{name:a}'
5479
```
5580

56-
### ~~Filters~~ Middleware:
57-
Is identical to filters, we change the name of `filter` to `middleware`.
58-
`Route::newMiddleware('auth', Auth::class, 'auth')` is the basic syntax for adding a middleware to our RouteCollector object. The first parameter is the name of the middleware. The second parameter is the class referenced and the third parameter the name of the function it belongs to. <br>
59-
60-
```php
61-
'middleware' => [
62-
Route::newMiddleware('auth', Auth::class, 'auth'),
63-
Route::newMiddleware('no-auth', Auth::class, 'auth')
64-
]
65-
```
66-
67-
When calling `Route::middleware()` keep in mind that the first parameter is an array loaded with data. <br>
68-
69-
The first index is the middleware at position `before`. <br>
70-
The second index is optional and points to `after`. <br>
71-
The third index is optional and indicates a `prefix` to work the middleware in a more dynamic way. <br>
72-
73-
Take into account that if more than 3 parameters are added, these are left over and do not generate internal errors in their operation.
74-
```php
75-
use LionRoute\Route;
76-
use Example\Auth;
77-
78-
Route::init([
79-
'middleware' => [
80-
Route::newMiddleware('auth', Auth::class, 'auth'),
81-
Route::newMiddleware('no-auth', Auth::class, 'auth')
82-
]
83-
]);
84-
85-
Route::middleware(['no-auth'], function() {
86-
Route::post('login', function() {
87-
return [
88-
'status' => "success",
89-
'message' => "Hello world."
90-
];
91-
});
92-
});
93-
```
94-
95-
### Prefix Groups:
96-
```php
97-
Route::prefix('authenticate', function() {
98-
Route::post('login', function() {
99-
return [
100-
'status' => "success",
101-
'message' => "Hello world."
102-
];
103-
});
104-
});
105-
```
106-
10781
### Example methods:
10882
#### GET
10983
```php
84+
use App\Http\Controllers\Home\Example;
85+
11086
Route::get('/example-url', function() {
11187
$get = new Example();
11288
$get->getMethod();
@@ -119,6 +95,8 @@ Route::get('/example-url', [Example::class, 'getMethod']);
11995

12096
#### POST
12197
```php
98+
use App\Http\Controllers\Home\Example;
99+
122100
Route::post('/example-url', function() {
123101
$post = new Example();
124102
$post->postMethod();
@@ -131,6 +109,8 @@ Route::post('/example-url', [Example::class, 'postMethod']);
131109

132110
#### PUT
133111
```php
112+
use App\Http\Controllers\Home\Example;
113+
134114
Route::put('/example-url/{id}', function($id) {
135115
$put = new Example();
136116
$put->putMethod();
@@ -143,6 +123,8 @@ Route::put('/example-url/{id}', [Example::class, 'putMethod']);
143123

144124
#### DELETE
145125
```php
126+
use App\Http\Controllers\Home\Example;
127+
146128
Route::delete('/example-url/{id}', function($id) {
147129
$delete = new Example();
148130
$delete->deleteMethod();
@@ -155,6 +137,8 @@ Route::delete('/example-url/{id}', [Example::class, 'deleteMethod']);
155137

156138
#### ANY
157139
```php
140+
use App\Http\Controllers\Home\Example;
141+
158142
Route::any('/example-url', function($id) {
159143
$any = new Example();
160144
$any->anyMethod();
@@ -165,6 +149,115 @@ Route::any('/example-url', function($id) {
165149
Route::any('/example-url', [Example::class, 'anyMethod']);
166150
```
167151

152+
### ~~Filters~~ Middleware:
153+
It's identical to filters, we renamed `filter` to `middleware`. `['auth', Auth::class, 'auth']` is the basic syntax for adding a middleware to our RouteCollector object. Each middleware must be encapsulated in an array, where each middleware carries its information within another array. The first parameter is the name of the middleware. The second parameter is the class being referenced and the third parameter the name of the function it belongs to. <br>
154+
155+
```php
156+
use LionRoute\Route;
157+
use App\Http\Middleware\Auth;
158+
159+
Route::init()->newMiddleware([
160+
['auth', Auth::class, 'auth'],
161+
['no-auth', Auth::class, 'noAuth']
162+
]);
163+
```
164+
165+
```php
166+
// Auth Class
167+
168+
namespace App\Http\Middleware;
169+
170+
class Auth {
171+
172+
public function __construct() {
173+
174+
}
175+
176+
public function auth(): void {
177+
if (!isset($_SESSION['user_session'])) {
178+
echo(json_encode([
179+
'status' => "error",
180+
'message' => "user session does not exist"
181+
]));
182+
183+
exit(); // exit to end the execution of the process up to that point.
184+
}
185+
}
186+
187+
public function noAuth(): void {
188+
if (isset($_SESSION['user_session'])) {
189+
echo(json_encode([
190+
'status' => "error",
191+
'message' => "user session exists"
192+
]));
193+
194+
exit(); // exit to end the execution of the process up to that point.
195+
}
196+
}
197+
198+
}
199+
```
200+
201+
When calling `Route::middleware()` keep in mind that the first parameter is an array loaded with data. <br>
202+
203+
The first index is the middleware at position `before`. <br>
204+
The second index is optional and points to `after`. <br>
205+
The third index is optional and indicates a `prefix` to work the middleware in a more dynamic way. <br>
206+
207+
Take into account that if more than 3 parameters are added, these are left over and do not generate internal errors in their operation.
208+
209+
```php
210+
use App\Http\Controllers\Home\Example;
211+
212+
Route::middleware(['no-auth'], function() {
213+
Route::post('login', function() {
214+
return [
215+
'status' => "success",
216+
'message' => "Hello world."
217+
];
218+
});
219+
});
220+
221+
// or
222+
223+
Route::middleware(['no-auth'], function() {
224+
Route::post('login', [Example::class, 'postMethod']);
225+
});
226+
227+
// or
228+
229+
Route::post('login', function() {
230+
return [
231+
'status' => "success",
232+
'message' => "Hello world."
233+
];
234+
}, ['no-auth']);
235+
236+
// or
237+
238+
Route::post('login', [Example::class, 'postMethod'], ['no-auth']);
239+
```
240+
241+
### Prefix Groups:
242+
```php
243+
Route::prefix('authenticate', function() {
244+
Route::post('login', function() {
245+
return [
246+
'status' => "success",
247+
'message' => "Hello world."
248+
];
249+
});
250+
});
251+
252+
Route::prefix('reports', function() {
253+
Route::middleware(['auth'], function() {
254+
Route::post('excel', [Example::class, 'excelMethod']);
255+
Route::post('word', [Example::class, 'wordMethod']);
256+
Route::post('power-point', [Example::class, 'powerPointMethod']);
257+
});
258+
});
259+
```
260+
168261
## Credits
169262
[PHRoute](https://github.com/mrjgreen/phroute)
170263

composer.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
"LionRoute\\": "src/LionRoute/"
99
}
1010
},
11-
"autoload-dev": {
12-
"psr-4": {
13-
"App\\": "app/"
14-
}
15-
},
1611
"authors": [
1712
{
1813
"name": "Sergio León",

src/LionRoute/Route.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ public function __construct() {
1515

1616
}
1717

18-
public static function init(array $filters = []): Route {
18+
public static function init(): Route {
1919
self::$router = new RouteCollector(new RouteParser());
2020
return new Route();
2121
}
2222

23-
// public static function getRoutes(): array {
24-
// return (array) self::$router;
25-
// }
23+
public static function getRoutes(): array {
24+
return (array) self::$router;
25+
}
2626

2727
public static function newMiddleware(array $middleware): void {
2828
if (count($middleware) > 0) {
@@ -74,27 +74,15 @@ public static function get(string $url, \Closure|array $controller_function, arr
7474
}
7575

7676
public static function post(string $url, \Closure|array $controller_function, array $filters = []): void {
77-
if (count($filters) > 0) {
78-
self::$router->post($url, $controller_function, $filters);
79-
} else {
80-
self::$router->post($url, $controller_function);
81-
}
77+
self::executeMethod('post', $url, $controller_function, $filters);
8278
}
8379

8480
public static function put(string $url, \Closure|array $controller_function, array $filters = []): void {
85-
if (count($filters) > 0) {
86-
self::$router->put($url, $controller_function, $filters);
87-
} else {
88-
self::$router->put($url, $controller_function);
89-
}
81+
self::executeMethod('put', $url, $controller_function, $filters);
9082
}
9183

9284
public static function delete(string $url, \Closure|array $controller_function, array $filters = []): void {
93-
if (count($filters) > 0) {
94-
self::$router->delete($url, $controller_function, $filters);
95-
} else {
96-
self::$router->delete($url, $controller_function);
97-
}
85+
self::executeMethod('delete', $url, $controller_function, $filters);
9886
}
9987

10088
public static function any(string $url, \Closure|array $controller_function, array $filters = []): void {

0 commit comments

Comments
 (0)