Skip to content

Commit 2a70377

Browse files
committed
redirect plugin
1 parent 7179acc commit 2a70377

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

config/route.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Mvc5\Plugin\Callback;
77
use Mvc5\Session\SessionMessages;
88
use Mvc5\Service\Context;
9+
use Plugin\Redirect;
910
use Psr\Http\Message\ResponseInterface as Response;
1011
use Valar\RedirectResponse;
1112

@@ -86,6 +87,10 @@
8687
]
8788
]
8889
],
90+
'redirect' => [
91+
'path' => '/redirect',
92+
'controller' => new Redirect('/home'),
93+
],
8994
'app' => [
9095
/*'host' => 'nexus.app.dev',
9196
'port' => 8000,*/

src/Plugin/Redirect.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
*
4+
*/
5+
6+
namespace Plugin;
7+
8+
use Mvc5\Arg;
9+
use Mvc5\Plugin\Call;
10+
use Mvc5\Plugin\Callback;
11+
12+
class Redirect
13+
extends Call
14+
{
15+
/**
16+
*
17+
*/
18+
const STATUS = 302;
19+
20+
/**
21+
* @param string $url
22+
* @param int|null $status
23+
* @param array $headers
24+
*/
25+
function __construct(string $url, int $status = null, array $headers = [])
26+
{
27+
parent::__construct([$this, '__invoke'], [$url, $status ?? static::STATUS, $headers]);
28+
}
29+
30+
/**
31+
* @param string $url
32+
* @param int $status
33+
* @param array $headers
34+
* @return mixed
35+
*/
36+
function __invoke(string $url, ?int $status, array $headers)
37+
{
38+
return new Callback(function(/*$argv*/) use($url, $status, $headers) {
39+
return $this->plugin(Arg::RESPONSE_REDIRECT, [$url, $status, $headers]);
40+
});
41+
}
42+
}

view/home/index.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<a class="btn btn-primary btn-lg" href="#" role="button" onclick="return info();">Info</a>
1616
<a class="btn btn-primary btn-lg" href="#" role="button" onclick="return about();">About</a>
1717
<a class="btn btn-primary btn-lg" href="#" role="button" onclick="return demo();">Demo</a>
18+
<a class="btn btn-primary btn-lg" href="/redirect" role="button">Redirect</a>
1819
</p>
1920
</div>
2021
<script type="text/javascript">

0 commit comments

Comments
 (0)