Skip to content

Commit 206c865

Browse files
committed
Added convenience redirect method that first tries to find a named or reverse-routed URI to redirect to. Fixes #4
1 parent 3bb98fa commit 206c865

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

system/Common.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,32 @@ function force_https(int $duration = 31536000, RequestInterface $request = null,
420420

421421
//--------------------------------------------------------------------
422422

423+
if (! function_exists('redirect'))
424+
{
425+
/**
426+
* Convenience method that works with the current global $request and
427+
* $router instances to redirect using named/reverse-routed routes
428+
* to determine the URL to go to. If nothing is found, will treat
429+
* as a traditional redirect and pass the string in, letting
430+
* $response->redirect() determine the correct method and code.
431+
*
432+
* If more control is needed, you must use $response->redirect explicitly.
433+
*
434+
* @param string $uri
435+
*/
436+
function redirect (string $uri, ...$params)
437+
{
438+
global $response, $routes;
439+
440+
if ($route = $routes->reverseRoute($uri, ...$params))
441+
{
442+
$uri = $route;
443+
}
444+
445+
$response->redirect($uri);
446+
exit(EXIT_SUCCESS);
447+
}
448+
}
449+
450+
//--------------------------------------------------------------------
451+

0 commit comments

Comments
 (0)