Skip to content

Commit 36ebd98

Browse files
committed
Redirects model
1 parent 9e419c5 commit 36ebd98

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"php": "^8.4",
2020
"spatie/laravel-package-tools": "^1.16",
2121
"illuminate/contracts": "^10.0||^11.0||^12.0",
22-
"statikbe/laravel-filament-flexible-content-blocks": "^2.4"
22+
"statikbe/laravel-filament-flexible-content-blocks": "^2.4",
23+
"spatie/laravel-missing-page-redirector": "^2.11"
2324
},
2425
"require-dev": {
2526
"laravel/pint": "^1.14",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
public function up()
9+
{
10+
Schema::create('redirects', function (Blueprint $table) {
11+
$table->id();
12+
$table->string('old_url');
13+
$table->string('new_url');
14+
$table->integer('status_code')->nullable();
15+
$table->timestamps();
16+
});
17+
}
18+
};

src/Models/Redirect.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Statikbe\FilamentFlexibleContentBlockPages\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Facades\Cache;
8+
9+
/**
10+
* TODO observer
11+
* @see RedirectObserver for clearing the cache.
12+
*/
13+
class Redirect extends Model
14+
{
15+
use HasFactory;
16+
17+
const CACHE_REDIRECTS_KEY = 'filament-flexible-content-block-pages:redirects';
18+
19+
protected $guarded = ['id'];
20+
21+
/**
22+
* Returns a list of old and new urls with the status code if set compatible with spatie/laravel-missing-page-redirector,
23+
* that merges the redirects set in the database over the redirects set in the config.
24+
* @return array
25+
*/
26+
public static function getDirectionMap(): array {
27+
// Get from the database and remember forever
28+
// we clear this on new model or updated model
29+
$dbRedirects = Cache::rememberForever(static::CACHE_REDIRECTS_KEY, function () {
30+
return Redirect::all()->flatMap(function (Redirect $redirect) {
31+
if($redirect->status_code){
32+
return [
33+
$redirect->old_url => [$redirect->new_url, $redirect->status_code]
34+
];
35+
}
36+
else return [$redirect->old_url => $redirect->new_url];
37+
})->toArray();
38+
});
39+
40+
// Get the redirects from the config
41+
$configRedirects = config('missing-page-redirector.redirects');
42+
43+
// Merge both values
44+
return array_merge($configRedirects, $dbRedirects);
45+
}
46+
}

0 commit comments

Comments
 (0)