Skip to content

Commit 9a80ad3

Browse files
authored
Merge pull request #4 from dicoding-dev/feature/implements-route-redirect-method
Backport `Route::redirect()` method
2 parents 247537e + 433fd15 commit 9a80ad3

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Illuminate\Routing;
4+
5+
use Illuminate\Http\RedirectResponse;
6+
7+
class RedirectController extends Controller
8+
{
9+
public function action($destination, $status)
10+
{
11+
return new RedirectResponse($destination, $status);
12+
}
13+
}

src/Illuminate/Routing/Route.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected function compileRoute()
142142
$uri = preg_replace('/\{(\w+?)\?\}/', '{$1}', $this->uri);
143143

144144
$this->compiled = (
145-
new SymfonyRoute($uri, $optionals, $this->wheres, array(), $this->domain() ?: '')
145+
new SymfonyRoute($uri, $optionals, $this->wheres, ['utf8' => true], $this->domain() ?: '')
146146
)->compile();
147147
}
148148

@@ -455,11 +455,16 @@ protected function matchToKeys(array $matches)
455455
*/
456456
protected function replaceDefaults(array $parameters)
457457
{
458-
foreach ($parameters as $key => &$value)
459-
{
460-
$value = isset($value) ? $value : array_get($this->defaults, $key);
458+
foreach ($parameters as $key => $value) {
459+
$parameters[$key] = $value ?? array_get($this->defaults, $key);
461460
}
462461

462+
foreach ($this->defaults as $key => $value) {
463+
if (! isset($parameters[$key])) {
464+
$parameters[$key] = $value;
465+
}
466+
}
467+
463468
return $parameters;
464469
}
465470

src/Illuminate/Routing/Router.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,21 @@ public function any($uri, $action)
220220
return $this->addRoute($verbs, $uri, $action);
221221
}
222222

223+
/**
224+
* Create a redirect from one URI to another.
225+
*
226+
* @param string $uri
227+
* @param string $destination
228+
* @param int $status
229+
* @return \Illuminate\Routing\Route
230+
*/
231+
public function redirect($uri, $destination, $status = 301)
232+
{
233+
return $this->any($uri, '\Illuminate\Routing\RedirectController@action')
234+
->defaults('destination', $destination)
235+
->defaults('status', $status);
236+
}
237+
223238
/**
224239
* Register a new route with the given verbs.
225240
*

tests/Routing/RoutingRouteTest.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,9 @@ public function testBasicDispatchingOfRoutes(): void
8989
$router->get('foo/bar', fn() => 'second');
9090
$this->assertEquals('second', $router->dispatch(Request::create('foo/bar', 'GET'))->getContent());
9191

92-
// @todo fix this thest onnce you understand it
93-
// $router = $this->getRouter();
94-
// $router->get('foo/bar/åαф', function() { return 'hello'; });
95-
// $this->assertEquals('hello', $router->dispatch(Request::create('foo/bar/%C3%A5%CE%B1%D1%84', 'GET'))->getContent());
92+
$router = $this->getRouter();
93+
$router->get('foo/bar/åαф', function() { return 'hello'; });
94+
$this->assertEquals('hello', $router->dispatch(Request::create('foo/bar/%C3%A5%CE%B1%D1%84', 'GET'))->getContent());
9695
}
9796

9897

@@ -868,6 +867,37 @@ public function testRouterPatternSetting(): void
868867
}
869868

870869

870+
public function testRouteParametersDefaultValue()
871+
{
872+
$router = $this->getRouter();
873+
874+
$router->get('foo/{bar?}', function ($bar = '') {
875+
return $bar;
876+
})->defaults('bar', 'foo');
877+
$this->assertEquals('foo', $router->dispatch(Request::create('foo', 'GET'))->getContent());
878+
879+
880+
$router->get('foo/{bar?}', function ($bar = '') {
881+
return $bar;
882+
})->defaults('bar', 'foo');
883+
$this->assertEquals('bar', $router->dispatch(Request::create('foo/bar', 'GET'))->getContent());
884+
}
885+
886+
887+
public function testRouteRedirect()
888+
{
889+
$router = $this->getRouter();
890+
$router->get('contact_us', function () {
891+
throw new \Exception('Route should not be reachable.');
892+
});
893+
$router->redirect('contact_us', 'contact', 302);
894+
895+
$response = $router->dispatch(Request::create('contact_us', 'GET'));
896+
$this->assertTrue($response->isRedirect('contact'));
897+
$this->assertEquals(302, $response->getStatusCode());
898+
}
899+
900+
871901
protected function getRouter(): Router
872902
{
873903
return new Router(new Illuminate\Events\Dispatcher);

0 commit comments

Comments
 (0)