Skip to content

Commit

Permalink
Adding fallback routes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicollassilva committed Oct 11, 2021
1 parent 491c640 commit c15fe83
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ composer require nicollassilva/minasrouter
- [Route Redirect](https://github.com/nicollassilva/minasrouter#route-redirect)
- [Route with Individual Middleware in Group](https://github.com/nicollassilva/minasrouter#route-with-individual-middleware-in-group)
- [Route with Different Name in Group](https://github.com/nicollassilva/minasrouter#route-with-different-name-in-group)
- [Fallback Routes](https://github.com/nicollassilva/minasrouter#fallback-routes)

### 3. Request Route
- [Introduction](https://github.com/nicollassilva/minasrouter#request-route)
Expand Down Expand Up @@ -455,6 +456,18 @@ Route::permanentRedirect("/here", "/there");
// You can return an existing route
Route::redirect("/index", "web.index");
```

### Fallback Routes

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.

```php
Route::fallback(function() {
echo 'Route error!';
// ...
});
```

> 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.
Be careful you redirect to an existing route, because if it has dynamic arguments, it will return the entire regex and error returned.
Expand Down
25 changes: 18 additions & 7 deletions src/Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function get(String $uri, $callback)

/**
* @param string $uri
* @param \Closure|array $callback
* @param \Closure|array|string $callback
*
* @return \MinasRouter\Router\RouteManager
*/
Expand All @@ -62,7 +62,7 @@ public static function post(String $uri, $callback)

/**
* @param string $uri
* @param \Closure|array $callback
* @param \Closure|array|string $callback
*
* @return \MinasRouter\Router\RouteManager
*/
Expand All @@ -73,7 +73,7 @@ public static function put(String $uri, $callback)

/**
* @param string $uri
* @param \Closure|array $callback
* @param \Closure|array|string $callback
*
* @return \MinasRouter\Router\RouteManager
*/
Expand All @@ -84,7 +84,7 @@ public static function patch(String $uri, $callback)

/**
* @param string $uri
* @param \Closure|array $callback
* @param \Closure|array|string $callback
*
* @return \MinasRouter\Router\RouteManager
*/
Expand All @@ -106,7 +106,8 @@ public static function any(String $uri, $callback)

/**
* @param string $uri
* @param \Closure|array $callback
* @param string $redirect
* @param int $httpCode
*
* @return \MinasRouter\Router\RouteManager
*/
Expand All @@ -117,7 +118,7 @@ public static function redirect(String $uri, String $redirect, Int $httpCode = 3

/**
* @param string $uri
* @param \Closure|array $callback
* @param string $redirect
*
* @return \MinasRouter\Router\RouteManager
*/
Expand All @@ -132,7 +133,7 @@ public static function permanentRedirect(String $uri, String $redirect)
*
* @return \MinasRouter\Router\RouteManager
*/
public static function match(array $methods, String $uri, $callback)
public static function match(Array $methods, String $uri, $callback)
{
return self::$collection->addMultipleHttpRoutes($uri, $callback, $methods);
}
Expand All @@ -157,4 +158,14 @@ public static function execute()
{
self::$collection->run();
}

/**
* @param \Closure|array|string
*
* @return void
*/
public static function fallback($callback)
{
self::$collection->addRoute("GET", "/404", $callback)->name('fallback');
}
}
18 changes: 17 additions & 1 deletion src/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ protected function resolveRouterUri(String $uri): String
protected function dispatchRoute(): ?\Closure
{
if (!$route = $this->currentRoute) {
if($fallbackRoute = $this->getByName('fallback')) {
return $this->executeRoute($fallbackRoute);
}

$this->setHttpCode($this->httpCodes["notFound"]);

$this->throwException(
Expand Down Expand Up @@ -361,6 +365,18 @@ protected function dispatchRoute(): ?\Closure
return null;
}

/**
* Responsible for execute the route
*
* @param RouteManager $route
*
* @return mixed|false
*/
protected function executeRoute(RouteManager $route)
{
return call_user_func($route->getAction(), ...$route->closureReturn());
}

/**
* Method responsible for checking if the controller
* class exists and returns an instance of it.
Expand All @@ -387,7 +403,7 @@ protected function resolveRouteController(String $controller)
* Method responsible for executing
* the middlewares of the current route.
*
* @return void
* @return mixed|false|void
*/
protected function executeMiddlewares(RouteManager $route)
{
Expand Down

0 comments on commit c15fe83

Please sign in to comment.