-
Notifications
You must be signed in to change notification settings - Fork 1
/
UrlBehavior.php
43 lines (35 loc) · 944 Bytes
/
UrlBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace mrssoft\redirect;
use Yii;
use yii\base\Behavior;
use yii\web\Response;
class UrlBehavior extends Behavior
{
/**
* @var string
*/
public $redirect = 'redirect';
public function events()
{
return [
Response::EVENT_BEFORE_SEND => 'beforeSend'
];
}
public function beforeSend()
{
if (Yii::$app->response->statusCode == 404) {
$pathInfo = Yii::$app->request->pathInfo;
if (!empty($pathInfo)) {
/** @var \mrssoft\redirect\UrlRedirect $redirect */
$redirect = Yii::$app->get($this->redirect);
if ($redirect) {
$new = $redirect->find('/' . $pathInfo);
if ($new) {
header('Location: ' . $new, true, $redirect->code);
exit;
}
}
}
}
}
}