Skip to content

Commit

Permalink
redirect route using route name
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Oct 14, 2023
1 parent e5d1248 commit 5f91255
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/System/Http/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(string $url, int $response_code = 302, array $header
$this->setTarget($url);
}

public function setTarget(string $url)
public function setTarget(string $url): void
{
$this->setContent(sprintf('<html><head><meta charset="UTF-8" /><meta http-equiv="refresh" content="0;url=\'%1$s\'" /><title>Redirecting to %1$s</title></head><body>Redirecting to <a href="%1$s">%1$s</a>.</body></html>', htmlspecialchars($url, \ENT_QUOTES, 'UTF-8')));
$this->setHeaders([
Expand Down
35 changes: 35 additions & 0 deletions src/System/Integrate/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

// path aplication

use System\Http\RedirectResponse;
use System\Integrate\Exceptions\ApplicationNotAvailable;
use System\Router\Router;

if (!function_exists('app_path')) {
/**
Expand Down Expand Up @@ -310,3 +312,36 @@ function vite(...$entry_ponits)
return $vite(...$entry_ponits);
}
}

if (!function_exists('redirect_route')) {
/**
* Redirect to another route.
*
* @param string[] $parameter Dinamic parameter to fill with url exprestion
*/
function redirect_route(string $route_name, array $parameter = []): RedirectResponse
{
$route = Router::redirect($route_name);
$valueIndex = 0;
$url = preg_replace_callback(
"/\(:\w+\)/",
function ($matches) use ($parameter, &$valueIndex) {
if (!array_key_exists($matches[0], Router::$patterns)) {
throw new \Exception('parameter not matches with any pattern.');
}

if ($valueIndex < count($parameter)) {
$value = $parameter[$valueIndex];
$valueIndex++;

return $value;
}

return '';
},
$route['uri']
);

return new RedirectResponse($url);
}
}
3 changes: 2 additions & 1 deletion src/System/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public static function redirect(string $to): Route
{
foreach (self::$routes as $name => $route) {
if ($route['name'] === $to) {
return static::$routes[$name];
return self::$routes[$name];
}
}

Expand Down Expand Up @@ -324,6 +324,7 @@ public static function match($method, string $uri, $callback): Route

return self::$routes[] = new Route([
'method' => $method,
'uri' => $uri,
'expression' => self::mapPatterns($uri),
'function' => $callback,
'middleware' => $middleware,
Expand Down
55 changes: 55 additions & 0 deletions tests/Integrate/Helper/RedirectResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace System\Test\Integrate\Helper;

use PHPUnit\Framework\TestCase;
use System\Integrate\Testing\TestResponse;
use System\Router\Router;

final class RedirectResponseTest extends TestCase
{
/**
* @test
*/
public function itRiderectToCorrectUrl()
{
Router::get('/test/(:any)', fn ($test) => $test)->name('test');
$redirect = redirect_route('test', ['ok']);
$res = new TestResponse($redirect);
$res->assertStatusCode(302);

Router::reset();
}

/**
* @test
*/
public function itRiderectToCorrectUrlWithPlanUrl()
{
Router::get('/test', fn ($test) => $test)->name('test');
$redirect = redirect_route('test');
$res = new TestResponse($redirect);
$res->assertStatusCode(302);

Router::reset();
}

/**
* @test
*/
public function itThrowErrorWhenPatternNotExist()
{
Router::get('/test/(:test)', fn ($test) => $test)->name('test');
$message = '';
try {
redirect_route('test', ['test']);
} catch (\Throwable $th) {
$message = $th->getMessage();
}
$this->assertEquals('parameter not matches with any pattern.', $message);

Router::reset();
}
}

0 comments on commit 5f91255

Please sign in to comment.