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
|
|
|
/** |
29
|
|
|
* The attributes that are mass assignable. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $fillable = ['old_url', 'new_url', 'status']; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The attributes that should be cast to native types. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $casts = [ |
41
|
|
|
'status' => 'integer', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/* ----------------------------------------------------------------- |
45
|
|
|
| Constructor |
46
|
|
|
| ----------------------------------------------------------------- |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Meta constructor. |
51
|
|
|
* |
52
|
|
|
* @param array $attributes |
53
|
|
|
*/ |
54
|
4 |
|
public function __construct(array $attributes = []) |
55
|
|
|
{ |
56
|
4 |
|
parent::__construct($attributes); |
57
|
|
|
|
58
|
4 |
|
$this->setTable(Seo::getConfig('redirects.table', 'redirects')); |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The "booting" method of the model. |
63
|
|
|
*/ |
64
|
4 |
|
public static function boot() |
65
|
|
|
{ |
66
|
4 |
|
parent::boot(); |
67
|
|
|
|
68
|
|
|
// Extract these methods to Event/Listener Classes |
69
|
4 |
|
static::saved(function() { |
70
|
4 |
|
static::clearCache(); |
71
|
4 |
|
}); |
72
|
|
|
|
73
|
4 |
|
static::deleted(function() { |
74
|
2 |
|
static::clearCache(); |
75
|
4 |
|
}); |
76
|
4 |
|
} |
77
|
|
|
|
78
|
|
|
/* ----------------------------------------------------------------- |
79
|
|
|
| Getters & Setters |
80
|
|
|
| ----------------------------------------------------------------- |
81
|
|
|
*/ |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the `status_name` attribute. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
2 |
|
public function getStatusNameAttribute() |
89
|
|
|
{ |
90
|
2 |
|
return RedirectStatuses::get($this->status); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/* ----------------------------------------------------------------- |
94
|
|
|
| Main Methods |
95
|
|
|
| ----------------------------------------------------------------- |
96
|
|
|
*/ |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create a redirect url. |
100
|
|
|
* |
101
|
|
|
* @param string $oldUrl |
102
|
|
|
* @param string $newUrl |
103
|
|
|
* @param int $status |
104
|
|
|
* |
105
|
|
|
* @return \Arcanedev\LaravelSeo\Models\Redirect |
106
|
|
|
*/ |
107
|
4 |
|
public static function createOne($oldUrl, $newUrl, $status = Response::HTTP_MOVED_PERMANENTLY) |
108
|
|
|
{ |
109
|
4 |
|
$redirect = new self([ |
110
|
4 |
|
'old_url' => $oldUrl, |
111
|
4 |
|
'new_url' => $newUrl, |
112
|
4 |
|
'status' => $status, |
113
|
|
|
]); |
114
|
|
|
|
115
|
4 |
|
$redirect->save(); |
116
|
|
|
|
117
|
4 |
|
return $redirect; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/* ----------------------------------------------------------------- |
121
|
|
|
| Other Methods |
122
|
|
|
| ----------------------------------------------------------------- |
123
|
|
|
*/ |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Clear the cached data. |
127
|
|
|
*/ |
128
|
4 |
|
protected static function clearCache() |
129
|
|
|
{ |
130
|
4 |
|
cache()->forget( |
131
|
4 |
|
Seo::getConfig('redirector.drivers.eloquent.options.cache.key', 'seo-redirects') |
132
|
|
|
); |
133
|
4 |
|
} |
134
|
|
|
} |
135
|
|
|
|