1
|
|
|
<?php namespace Arcanedev\LaravelSeo\Models; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelSeo\Entities\RedirectStatuses; |
4
|
|
|
use Arcanedev\LaravelSeo\Seo; |
5
|
|
|
use Symfony\Component\HttpFoundation\Response; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Redirect |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\LaravelSeo\Models |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @property int id |
14
|
|
|
* @property string old_url |
15
|
|
|
* @property string new_url |
16
|
|
|
* @property int status |
17
|
|
|
* @property string status_name |
18
|
|
|
* @property \Carbon\Carbon created_at |
19
|
|
|
* @property \Carbon\Carbon updated_at |
20
|
|
|
*/ |
21
|
|
|
class Redirect extends AbstractModel |
22
|
|
|
{ |
23
|
|
|
/* ----------------------------------------------------------------- |
24
|
|
|
| Properties |
25
|
|
|
| ----------------------------------------------------------------- |
26
|
|
|
*/ |
27
|
|
|
/** |
28
|
|
|
* The attributes that are mass assignable. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $fillable = ['old_url', 'new_url', 'status']; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The attributes that should be cast to native types. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $casts = [ |
40
|
|
|
'status' => 'integer', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/* ----------------------------------------------------------------- |
44
|
|
|
| Constructor |
45
|
|
|
| ----------------------------------------------------------------- |
46
|
|
|
*/ |
47
|
|
|
/** |
48
|
|
|
* Meta constructor. |
49
|
|
|
* |
50
|
|
|
* @param array $attributes |
51
|
|
|
*/ |
52
|
6 |
|
public function __construct(array $attributes = []) |
53
|
|
|
{ |
54
|
6 |
|
parent::__construct($attributes); |
55
|
|
|
|
56
|
6 |
|
$this->setTable(Seo::getConfig('redirects.table', 'redirects')); |
57
|
6 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The "booting" method of the model. |
61
|
|
|
*/ |
62
|
6 |
|
public static function boot() |
63
|
|
|
{ |
64
|
6 |
|
parent::boot(); |
65
|
|
|
|
66
|
|
|
static::saved(function() { |
67
|
6 |
|
static::clearCache(); |
68
|
6 |
|
}); |
69
|
|
|
|
70
|
6 |
|
static::deleted(function() { |
71
|
|
|
static::clearCache(); |
72
|
6 |
|
}); |
73
|
6 |
|
} |
74
|
|
|
|
75
|
|
|
/* ----------------------------------------------------------------- |
76
|
|
|
| Getters & Setters |
77
|
|
|
| ----------------------------------------------------------------- |
78
|
|
|
*/ |
79
|
|
|
/** |
80
|
|
|
* Get the `status_name` attribute. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
3 |
|
public function getStatusNameAttribute() |
85
|
|
|
{ |
86
|
3 |
|
return RedirectStatuses::get($this->status); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/* ----------------------------------------------------------------- |
90
|
|
|
| Main Methods |
91
|
|
|
| ----------------------------------------------------------------- |
92
|
|
|
*/ |
93
|
|
|
/** |
94
|
|
|
* Create a redirect url. |
95
|
|
|
* |
96
|
|
|
* @param string $oldUrl |
97
|
|
|
* @param string $newUrl |
98
|
|
|
* @param int $status |
99
|
|
|
* |
100
|
|
|
* @return \Arcanedev\LaravelSeo\Models\Redirect |
101
|
|
|
*/ |
102
|
6 |
|
public static function createOne($oldUrl, $newUrl, $status = Response::HTTP_MOVED_PERMANENTLY) |
103
|
|
|
{ |
104
|
6 |
|
$redirect = new self([ |
105
|
6 |
|
'old_url' => $oldUrl, |
106
|
6 |
|
'new_url' => $newUrl, |
107
|
6 |
|
'status' => $status, |
108
|
2 |
|
]); |
109
|
|
|
|
110
|
6 |
|
$redirect->save(); |
111
|
|
|
|
112
|
6 |
|
return $redirect; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/* ----------------------------------------------------------------- |
116
|
|
|
| Other Methods |
117
|
|
|
| ----------------------------------------------------------------- |
118
|
|
|
*/ |
119
|
|
|
/** |
120
|
|
|
* Clear the cached data. |
121
|
|
|
*/ |
122
|
6 |
|
protected static function clearCache() |
123
|
|
|
{ |
124
|
6 |
|
$key = Seo::getConfig('redirector.drivers.eloquent.options.cache.key', 'seo-redirects'); |
125
|
|
|
|
126
|
6 |
|
cache()->forget($key); |
127
|
6 |
|
} |
128
|
|
|
} |
129
|
|
|
|