|
1
|
|
|
<?php namespace Arcanedev\LaravelSeo\Http\Middleware; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelSeo\Contracts\RedirectorFactory; |
|
4
|
|
|
use Closure; |
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class RedirectsMissingPages |
|
10
|
|
|
* |
|
11
|
|
|
* @package Arcanedev\LaravelSeo\Http\Middleware |
|
12
|
|
|
* @author ARCANEDEV <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class RedirectsMissingPages |
|
15
|
|
|
{ |
|
16
|
|
|
/* ----------------------------------------------------------------- |
|
17
|
|
|
| Properties |
|
18
|
|
|
| ----------------------------------------------------------------- |
|
19
|
|
|
*/ |
|
20
|
|
|
/** @var \Arcanedev\LaravelSeo\Contracts\Redirector */ |
|
21
|
|
|
protected $redirector; |
|
22
|
|
|
|
|
23
|
|
|
/* ----------------------------------------------------------------- |
|
24
|
|
|
| Constructor |
|
25
|
|
|
| ----------------------------------------------------------------- |
|
26
|
|
|
*/ |
|
27
|
|
|
/** |
|
28
|
|
|
* RedirectsMissingPages constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Arcanedev\LaravelSeo\Contracts\RedirectorFactory $redirectorManager |
|
31
|
|
|
*/ |
|
32
|
24 |
|
public function __construct(RedirectorFactory $redirectorManager) |
|
33
|
|
|
{ |
|
34
|
24 |
|
$this->redirector = $redirectorManager->driver(); |
|
35
|
24 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/* ----------------------------------------------------------------- |
|
38
|
|
|
| Main Methods |
|
39
|
|
|
| ----------------------------------------------------------------- |
|
40
|
|
|
*/ |
|
41
|
|
|
/** |
|
42
|
|
|
* Handle the missing pages redirection. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Illuminate\Http\Request $request |
|
45
|
|
|
* @param \Closure $next |
|
46
|
|
|
* |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
24 |
|
public function handle(Request $request, Closure $next) |
|
50
|
|
|
{ |
|
51
|
24 |
|
$response = $next($request); |
|
52
|
|
|
|
|
53
|
24 |
|
if ($response->getStatusCode() !== Response::HTTP_NOT_FOUND) |
|
54
|
14 |
|
return $response; |
|
55
|
|
|
|
|
56
|
18 |
|
$redirectResponse = $this->redirector->getRedirectFor($request); |
|
57
|
|
|
|
|
58
|
18 |
|
return $redirectResponse ? $redirectResponse : $response; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|