|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Support\Models; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Hyde; |
|
8
|
|
|
use Hyde\Pages\InMemoryPage; |
|
9
|
|
|
use Illuminate\Support\Facades\View; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* A basic redirect page. Is not discoverable by Hyde, instead you manually need to create the pages. |
|
13
|
|
|
* Typically, you'll do this in a build task. Pass data a new object, then call the store method. |
|
14
|
|
|
* The store method will then render the redirect page to the project's site output directory. |
|
15
|
|
|
* Once viewed in a web browser a meta refresh will redirect the user to the new location. |
|
16
|
|
|
* |
|
17
|
|
|
* Since redirects are not discoverable, they also never show up in navigation, sitemaps, etc. |
|
18
|
|
|
* If you want, you can however add the pages to the HydeKernel route index by adding it |
|
19
|
|
|
* in the boot method of your AppServiceProvider, or any other suitable location. |
|
20
|
|
|
* That way, your redirect will be saved by the standard build command. |
|
21
|
|
|
* |
|
22
|
|
|
* @example `Redirect::make('foo', 'bar')->store();` |
|
23
|
|
|
*/ |
|
24
|
|
|
class Redirect extends InMemoryPage |
|
25
|
|
|
{ |
|
26
|
|
|
public readonly string $path; |
|
27
|
|
|
public readonly string $destination; |
|
28
|
|
|
public readonly bool $showText; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create a new redirect page file in the project's site output directory. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $path The URI path to redirect from. |
|
34
|
|
|
* @param string $destination The destination to redirect to. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(string $path, string $destination, bool $showText = true) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->path = $this->normalizePath($path); |
|
|
|
|
|
|
39
|
|
|
$this->destination = $destination; |
|
|
|
|
|
|
40
|
|
|
$this->showText = $showText; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
parent::__construct($this->path); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function create(string $path, string $destination, bool $showText = true): static |
|
46
|
|
|
{ |
|
47
|
|
|
return (new static($path, $destination, $showText))->store(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function compile(): string |
|
51
|
|
|
{ |
|
52
|
|
|
return View::make('hyde::pages.redirect', [ |
|
53
|
|
|
'destination' => $this->destination, |
|
54
|
|
|
'showText' => $this->showText, |
|
55
|
|
|
])->render(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function store(): static |
|
59
|
|
|
{ |
|
60
|
|
|
file_put_contents(Hyde::sitePath("$this->path.html"), $this->compile()); |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function normalizePath(string $path): string |
|
66
|
|
|
{ |
|
67
|
|
|
if (str_ends_with($path, '.html')) { |
|
68
|
|
|
return substr($path, 0, -5); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $path; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|