Skip to content

Commit 198d9f6

Browse files
committed
Merge branch 'new'
2 parents dc2a7c2 + 80df78d commit 198d9f6

File tree

4 files changed

+111
-69
lines changed

4 files changed

+111
-69
lines changed

README.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# This library has a quick use of the router with regular expressions based on [mrjgreen phroute](https://github.com/mrjgreen/phroute).
1+
# This library has a quick use of the router with regular expressions based on [phroute](https://github.com/mrjgreen/phroute).
2+
23
[![Latest Stable Version](http://poser.pugx.org/lion-framework/lion-route/v)](https://packagist.org/packages/lion-framework/lion-route) [![Total Downloads](http://poser.pugx.org/lion-framework/lion-route/downloads)](https://packagist.org/packages/lion-framework/lion-route) [![Latest Unstable Version](http://poser.pugx.org/lion-framework/lion-route/v/unstable)](https://packagist.org/packages/lion-framework/lion-route) [![License](http://poser.pugx.org/lion-framework/lion-route/license)](https://packagist.org/packages/lion-framework/lion-route) [![PHP Version Require](http://poser.pugx.org/lion-framework/lion-route/require/php)](https://packagist.org/packages/lion-framework/lion-route)
34

45
## Install
@@ -32,6 +33,27 @@ composer require lion-framework/lion-route
3233
```
3334

3435
## Usage
36+
Start your development server
37+
```shell
38+
php -S localhost:40400
39+
```
40+
41+
It is recommended to start the development server yourself, since software such as `XAMPP, WampServer, BitNami WAMP Stack, Apache Lounge... etc`, provide directories in which to load your PHP projects, This results in running on the browser routes as `'localhost/MyProject/example'`.
42+
This generates a conflict since the route obtained comes by default as `'MyProject/example'`, something completely wrong. You can solve it by indicating from which parameter the URL can be obtained from the `Route::init()` method.
43+
44+
Indicate with an integer from which position the URL will be obtained, By default it is initialized to 1.
45+
```php
46+
/*
47+
myweb.com/auth/signin/example
48+
1 -> auth/signin/example
49+
2 -> signin/example
50+
3 -> example
51+
4+ ...
52+
*/
53+
Route::init(1);
54+
```
55+
56+
### DEFINING ROUTES
3557
```php
3658
require_once("vendor/autoload.php");
3759

@@ -46,11 +68,10 @@ Route::any('/', function() {
4668
];
4769
});
4870

49-
// 1 is for production and 2+ for local environment.
50-
Route::dispatch(2);
71+
Route::dispatch();
5172
```
5273

53-
### Defining routes:
74+
### DEFINITION OF ROUTE TYPES
5475
```php
5576
use LionRoute\Route;
5677

@@ -69,7 +90,7 @@ Route::patch($route, $handler);
6990

7091
This method accepts the HTTP method the route must match, the route pattern and a callable handler, which can be a closure, function name or `['ClassName', 'method']`. [more information in...](https://github.com/mrjgreen/phroute#defining-routes)
7192

72-
### Regex Shortcuts:
93+
### REGEX SHORTCUTS
7394
```
7495
:i => :/d+ # numbers only
7596
:a => :[a-zA-Z0-9]+ # alphanumeric
@@ -82,7 +103,7 @@ use in routes:
82103
'/user/{name:a}'
83104
```
84105

85-
### Example methods:
106+
### EXAMPLE METHODS
86107
#### GET
87108
```php
88109
use App\Http\Controllers\Home\Example;
@@ -153,7 +174,7 @@ Route::any('/example-url', function($id) {
153174
Route::any('/example-url', [Example::class, 'anyMethod']);
154175
```
155176

156-
### ~~Filters~~ Middleware:
177+
### ~~FILTERS~~ MIDDLEWARE:
157178
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>
158179

159180
```php
@@ -209,7 +230,6 @@ The second index is optional and points to `after`. <br>
209230
The third index is optional and indicates a `prefix` to work the middleware in a more dynamic way. <br>
210231

211232
Take into account that if more than 3 parameters are added, these are left over and do not generate internal errors in their operation.
212-
213233
```php
214234
use App\Http\Controllers\Home\Example;
215235

@@ -242,7 +262,7 @@ Route::post('login', function() {
242262
Route::post('login', [Example::class, 'postMethod'], ['no-auth']);
243263
```
244264

245-
### Prefix Groups:
265+
### PREFIX GROUPS:
246266
```php
247267
Route::prefix('authenticate', function() {
248268
Route::post('login', function() {
@@ -259,6 +279,8 @@ Route::prefix('reports', function() {
259279
Route::post('word', [Example::class, 'wordMethod']);
260280
Route::post('power-point', [Example::class, 'powerPointMethod']);
261281
});
282+
283+
Route::post('pdf', [Example::class, 'pdfMethod']);
262284
});
263285
```
264286

src/LionRoute/Config/RouteConfig.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace LionRoute\Config;
4+
5+
class RouteConfig {
6+
7+
public function __construct() {
8+
9+
}
10+
11+
public static function processInput(int $index): string {
12+
if ($index === 1) {
13+
return parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
14+
}
15+
16+
return implode('/', array_slice(explode('/', $_SERVER['REQUEST_URI']), $index));
17+
}
18+
19+
public static function processOutput($response): void {
20+
echo(json_encode($response));
21+
exit();
22+
}
23+
24+
}

src/LionRoute/Http.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,43 @@
22

33
namespace LionRoute;
44

5+
use \Closure;
56
use Phroute\Phroute\RouteCollector;
7+
use LionRoute\Config\RouteConfig;
68

79
class Http {
810

911
protected static RouteCollector $router;
12+
protected static int $index;
1013

1114
public function __construct() {
1215

1316
}
1417

15-
public static function get(string $url, \Closure|array $controller_function, array $filters = []): void {
18+
public static function prefix(string $prefix_name, Closure $closure): void {
19+
self::$router->group(['prefix' => $prefix_name], function($router) use ($closure) {
20+
$closure();
21+
});
22+
}
23+
24+
public static function middleware(array $middleware, Closure $closure): void {
25+
$count = count($middleware);
26+
$list_middleware = [];
27+
28+
if ($count === 1) {
29+
$list_middleware = ['before' => $middleware[0]];
30+
} elseif ($count === 2) {
31+
$list_middleware = ['before' => $middleware[0], 'after' => $middleware[1]];
32+
} elseif ($count >= 3) {
33+
$list_middleware = ['before' => $middleware[0], 'after' => $middleware[1], 'prefix' => $middleware[2]];
34+
}
35+
36+
self::$router->group($list_middleware, function($router) use ($closure) {
37+
$closure();
38+
});
39+
}
40+
41+
public static function get(string $url, Closure|array $controller_function, array $filters = []): void {
1642
if (count($filters) > 0) {
1743
self::$router->get(
1844
$url,
@@ -24,7 +50,7 @@ public static function get(string $url, \Closure|array $controller_function, arr
2450
}
2551
}
2652

27-
public static function post(string $url, \Closure|array $controller_function, array $filters = []): void {
53+
public static function post(string $url, Closure|array $controller_function, array $filters = []): void {
2854
if (count($filters) > 0) {
2955
self::$router->post(
3056
$url,
@@ -36,7 +62,7 @@ public static function post(string $url, \Closure|array $controller_function, ar
3662
}
3763
}
3864

39-
public static function put(string $url, \Closure|array $controller_function, array $filters = []): void {
65+
public static function put(string $url, Closure|array $controller_function, array $filters = []): void {
4066
if (count($filters) > 0) {
4167
self::$router->put(
4268
$url,
@@ -48,7 +74,7 @@ public static function put(string $url, \Closure|array $controller_function, arr
4874
}
4975
}
5076

51-
public static function delete(string $url, \Closure|array $controller_function, array $filters = []): void {
77+
public static function delete(string $url, Closure|array $controller_function, array $filters = []): void {
5278
if (count($filters) > 0) {
5379
self::$router->delete(
5480
$url,
@@ -60,7 +86,7 @@ public static function delete(string $url, \Closure|array $controller_function,
6086
}
6187
}
6288

63-
public static function any(string $url, \Closure|array $controller_function, array $filters = []): void {
89+
public static function any(string $url, Closure|array $controller_function, array $filters = []): void {
6490
if (count($filters) > 0) {
6591
self::$router->any(
6692
$url,
@@ -72,7 +98,7 @@ public static function any(string $url, \Closure|array $controller_function, arr
7298
}
7399
}
74100

75-
public static function head(string $url, \Closure|array $controller_function, array $filters = []): void {
101+
public static function head(string $url, Closure|array $controller_function, array $filters = []): void {
76102
if (count($filters) > 0) {
77103
self::$router->head(
78104
$url,
@@ -84,7 +110,7 @@ public static function head(string $url, \Closure|array $controller_function, ar
84110
}
85111
}
86112

87-
public static function options(string $url, \Closure|array $controller_function, array $filters = []): void {
113+
public static function options(string $url, Closure|array $controller_function, array $filters = []): void {
88114
if (count($filters) > 0) {
89115
self::$router->options(
90116
$url,
@@ -96,7 +122,7 @@ public static function options(string $url, \Closure|array $controller_function,
96122
}
97123
}
98124

99-
public static function patch(string $url, \Closure|array $controller_function, array $filters = []): void {
125+
public static function patch(string $url, Closure|array $controller_function, array $filters = []): void {
100126
if (count($filters) > 0) {
101127
self::$router->patch(
102128
$url,

src/LionRoute/Route.php

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
namespace LionRoute;
44

5+
use \Closure;
56
use Phroute\Phroute\{ RouteCollector, RouteParser, Dispatcher };
67
use Phroute\Phroute\Exception\{ HttpRouteNotFoundException, HttpMethodNotAllowedException };
7-
use LionRoute\Middleware;
8-
use LionRoute\{ Singleton, Http };
8+
use LionRoute\Config\RouteConfig;
9+
use LionRoute\{ Singleton, Http, Middleware };
910

1011
class Route extends Http {
1112

1213
use Singleton;
1314

1415
protected static array $addMiddleware = [];
1516

16-
public static function init(): Route {
17+
public static function init(int $index = 1): Route {
18+
self::$index = $index;
1719
self::$router = new RouteCollector();
1820
return self::getInstance();
1921
}
@@ -33,65 +35,33 @@ public static function newMiddleware(array $middleware): void {
3335
}
3436

3537
private static function createMiddleware(): void {
36-
if (count(self::$addMiddleware) > 0) {
37-
foreach (self::$addMiddleware as $key => $obj) {
38-
self::$router->filter($obj->getMiddlewareName(), function() use ($obj) {
39-
$objectClass = $obj->getNewObjectClass();
40-
$methodClass = $obj->getMethodClass();
41-
$objectClass->$methodClass();
42-
});
43-
}
44-
}
45-
}
46-
47-
public static function prefix(string $prefix_name, \Closure $closure): void {
48-
self::$router->group(['prefix' => $prefix_name], function($router) use ($closure) {
49-
$closure();
50-
});
51-
}
52-
53-
public static function middleware(array $middleware, \Closure $closure): void {
54-
$count = count($middleware);
55-
$list_middleware = [];
56-
57-
if ($count === 1) {
58-
$list_middleware = ['before' => $middleware[0]];
59-
} elseif ($count === 2) {
60-
$list_middleware = ['before' => $middleware[0], 'after' => $middleware[1]];
61-
} elseif ($count >= 3) {
62-
$list_middleware = ['before' => $middleware[0], 'after' => $middleware[1], 'prefix' => $middleware[2]];
38+
foreach (self::$addMiddleware as $key => $obj) {
39+
self::$router->filter($obj->getMiddlewareName(), function() use ($obj) {
40+
$objectClass = $obj->getNewObjectClass();
41+
$methodClass = $obj->getMethodClass();
42+
$objectClass->$methodClass();
43+
});
6344
}
64-
65-
self::$router->group($list_middleware, function($router) use ($closure) {
66-
$closure();
67-
});
68-
}
69-
70-
private static function processInput(int $index): string {
71-
return implode('/', array_slice(explode('/', $_SERVER['REQUEST_URI']), $index));
7245
}
7346

74-
private static function processOutput($response): void {
75-
echo(json_encode($response));
76-
exit();
77-
}
78-
79-
public static function dispatch(int $index): void {
47+
public static function dispatch(): void {
8048
try {
81-
self::processOutput(
49+
RouteConfig::processOutput(
8250
(new Dispatcher(self::$router->getData()))->dispatch(
8351
$_SERVER['REQUEST_METHOD'],
84-
self::processInput($index)
52+
RouteConfig::processInput(self::$index)
8553
)
8654
);
8755
} catch (HttpRouteNotFoundException $e) {
88-
self::processOutput(
89-
['status' => "error", 'message' => "Path not found: {$e->getMessage()}"]
90-
);
56+
RouteConfig::processOutput([
57+
'status' => "error",
58+
'message' => "Path not found: {$e->getMessage()}"
59+
]);
9160
} catch (HttpMethodNotAllowedException $e) {
92-
self::processOutput(
93-
['status' => "error", 'message' => "Method not allowed, {$e->getMessage()}"]
94-
);
61+
RouteConfig::processOutput([
62+
'status' => "error",
63+
'message' => "Method not allowed, {$e->getMessage()}"
64+
]);
9565
}
9666
}
9767

0 commit comments

Comments
 (0)