Skip to content

Commit c15fe83

Browse files
committed
Adding fallback routes
1 parent 491c640 commit c15fe83

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

Diff for: README.md

+13
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ composer require nicollassilva/minasrouter
8484
- [Route Redirect](https://github.com/nicollassilva/minasrouter#route-redirect)
8585
- [Route with Individual Middleware in Group](https://github.com/nicollassilva/minasrouter#route-with-individual-middleware-in-group)
8686
- [Route with Different Name in Group](https://github.com/nicollassilva/minasrouter#route-with-different-name-in-group)
87+
- [Fallback Routes](https://github.com/nicollassilva/minasrouter#fallback-routes)
8788

8889
### 3. Request Route
8990
- [Introduction](https://github.com/nicollassilva/minasrouter#request-route)
@@ -455,6 +456,18 @@ Route::permanentRedirect("/here", "/there");
455456
// You can return an existing route
456457
Route::redirect("/index", "web.index");
457458
```
459+
460+
### Fallback Routes
461+
462+
The fallback route is responsible when there is no route registered with that url address. Whenever there is no route that was requested by the user, the fallback route will be called.
463+
464+
```php
465+
Route::fallback(function() {
466+
echo 'Route error!';
467+
// ...
468+
});
469+
```
470+
458471
> OBS: Tenha cuidado caso queira redirecionar para uma rota existente, se nela conter argumentos dinâmicos, ela retornará todo o regex e irá causar erro.
459472
460473
Be careful you redirect to an existing route, because if it has dynamic arguments, it will return the entire regex and error returned.

Diff for: src/Router/Route.php

+18-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static function get(String $uri, $callback)
5151

5252
/**
5353
* @param string $uri
54-
* @param \Closure|array $callback
54+
* @param \Closure|array|string $callback
5555
*
5656
* @return \MinasRouter\Router\RouteManager
5757
*/
@@ -62,7 +62,7 @@ public static function post(String $uri, $callback)
6262

6363
/**
6464
* @param string $uri
65-
* @param \Closure|array $callback
65+
* @param \Closure|array|string $callback
6666
*
6767
* @return \MinasRouter\Router\RouteManager
6868
*/
@@ -73,7 +73,7 @@ public static function put(String $uri, $callback)
7373

7474
/**
7575
* @param string $uri
76-
* @param \Closure|array $callback
76+
* @param \Closure|array|string $callback
7777
*
7878
* @return \MinasRouter\Router\RouteManager
7979
*/
@@ -84,7 +84,7 @@ public static function patch(String $uri, $callback)
8484

8585
/**
8686
* @param string $uri
87-
* @param \Closure|array $callback
87+
* @param \Closure|array|string $callback
8888
*
8989
* @return \MinasRouter\Router\RouteManager
9090
*/
@@ -106,7 +106,8 @@ public static function any(String $uri, $callback)
106106

107107
/**
108108
* @param string $uri
109-
* @param \Closure|array $callback
109+
* @param string $redirect
110+
* @param int $httpCode
110111
*
111112
* @return \MinasRouter\Router\RouteManager
112113
*/
@@ -117,7 +118,7 @@ public static function redirect(String $uri, String $redirect, Int $httpCode = 3
117118

118119
/**
119120
* @param string $uri
120-
* @param \Closure|array $callback
121+
* @param string $redirect
121122
*
122123
* @return \MinasRouter\Router\RouteManager
123124
*/
@@ -132,7 +133,7 @@ public static function permanentRedirect(String $uri, String $redirect)
132133
*
133134
* @return \MinasRouter\Router\RouteManager
134135
*/
135-
public static function match(array $methods, String $uri, $callback)
136+
public static function match(Array $methods, String $uri, $callback)
136137
{
137138
return self::$collection->addMultipleHttpRoutes($uri, $callback, $methods);
138139
}
@@ -157,4 +158,14 @@ public static function execute()
157158
{
158159
self::$collection->run();
159160
}
161+
162+
/**
163+
* @param \Closure|array|string
164+
*
165+
* @return void
166+
*/
167+
public static function fallback($callback)
168+
{
169+
self::$collection->addRoute("GET", "/404", $callback)->name('fallback');
170+
}
160171
}

Diff for: src/Router/RouteCollection.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ protected function resolveRouterUri(String $uri): String
322322
protected function dispatchRoute(): ?\Closure
323323
{
324324
if (!$route = $this->currentRoute) {
325+
if($fallbackRoute = $this->getByName('fallback')) {
326+
return $this->executeRoute($fallbackRoute);
327+
}
328+
325329
$this->setHttpCode($this->httpCodes["notFound"]);
326330

327331
$this->throwException(
@@ -361,6 +365,18 @@ protected function dispatchRoute(): ?\Closure
361365
return null;
362366
}
363367

368+
/**
369+
* Responsible for execute the route
370+
*
371+
* @param RouteManager $route
372+
*
373+
* @return mixed|false
374+
*/
375+
protected function executeRoute(RouteManager $route)
376+
{
377+
return call_user_func($route->getAction(), ...$route->closureReturn());
378+
}
379+
364380
/**
365381
* Method responsible for checking if the controller
366382
* class exists and returns an instance of it.
@@ -387,7 +403,7 @@ protected function resolveRouteController(String $controller)
387403
* Method responsible for executing
388404
* the middlewares of the current route.
389405
*
390-
* @return void
406+
* @return mixed|false|void
391407
*/
392408
protected function executeMiddlewares(RouteManager $route)
393409
{

0 commit comments

Comments
 (0)