diff --git a/app/Entity/Post.php b/app/Entity/Post.php index 0b8c60d08a8..7725ec50dbc 100644 --- a/app/Entity/Post.php +++ b/app/Entity/Post.php @@ -21,6 +21,7 @@ public function __construct( private string $content, private ?DateTimeInterface $updatedAt, private ?string $updatedMessage, + private ?string $alias, ) { } @@ -74,11 +75,6 @@ public function getUpdatedMessage(): ?string return $this->updatedMessage; } - // public function getYear(): int - // { - // return (int) $this->dateTime->format('Y'); - // } - public function getContent(): string { return $this->content; @@ -95,4 +91,9 @@ public function hasTweets(): bool { return str_contains($this->content, 'class="twitter-tweet"'); } + + public function getAlias(): ?string + { + return $this->alias; + } } diff --git a/app/EntityFactory/PostFactory.php b/app/EntityFactory/PostFactory.php index f052b018258..0cdb82cafd7 100644 --- a/app/EntityFactory/PostFactory.php +++ b/app/EntityFactory/PostFactory.php @@ -66,6 +66,8 @@ public function createFromFilePath(string $filePath): Post $matches['content'], $updatedAt, $configuration['updated_message'] ?? null, + $configuration['alias'] ?? null, + ); } } diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index decdaf3a21a..b07986bdc95 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -6,6 +6,7 @@ use App\Repository\PostRepository; use Illuminate\Contracts\View\View; +use Illuminate\Http\RedirectResponse; use Illuminate\Routing\Controller; final class PostController extends Controller @@ -15,10 +16,14 @@ public function __construct( ) { } - public function __invoke(string $slug): View + public function __invoke(string $slug): View|RedirectResponse { $post = $this->postRepository->getBySlug($slug); + if ($post->getAlias() && $post->getAlias() !== $slug) { + return redirect('/blog/' . $post->getAlias()); + } + return \view('post', [ 'post' => $post, 'title' => $post->getClearTitle(), diff --git a/app/Repository/PostRepository.php b/app/Repository/PostRepository.php index 14a2eaffc22..9d99c2c3eef 100644 --- a/app/Repository/PostRepository.php +++ b/app/Repository/PostRepository.php @@ -38,6 +38,10 @@ public function getBySlug(string $slug): Post { $slug = rtrim($slug, '/'); foreach ($this->posts as $post) { + if ($post->getAlias() === $slug) { + return $post; + } + $postSlug = rtrim($post->getSlug(), '/'); if ($postSlug === $slug) { return $post; diff --git a/resources/posts/2018/2018-07-30-hidden-gems-of-php-packages-nette-utils.md b/resources/posts/2018/2018-07-30-hidden-gems-of-php-packages-nette-utils.md index 3a72edfbee9..5d2fb8fee3f 100644 --- a/resources/posts/2018/2018-07-30-hidden-gems-of-php-packages-nette-utils.md +++ b/resources/posts/2018/2018-07-30-hidden-gems-of-php-packages-nette-utils.md @@ -6,6 +6,8 @@ perex: | Today we start with [Nette\Utils](https://github.com/nette/utils) package. + +alias: "php-gems-nette-utils" --- ## Why I Use it diff --git a/resources/posts/2018/2018-08-13-hidden-gems-of-php-packages-symfony-finder-and-spl-file-info.md b/resources/posts/2018/2018-08-13-hidden-gems-of-php-packages-symfony-finder-and-spl-file-info.md index 6cf45d65be1..e4a845b6f0e 100644 --- a/resources/posts/2018/2018-08-13-hidden-gems-of-php-packages-symfony-finder-and-spl-file-info.md +++ b/resources/posts/2018/2018-08-13-hidden-gems-of-php-packages-symfony-finder-and-spl-file-info.md @@ -5,6 +5,7 @@ perex: | The series on not-so-well-known packages that might save your ass more than you think continues. Today we look on **files as objects**. +alias: php-gems-symfony-finder --- diff --git a/routes/web.php b/routes/web.php index af14608db97..5b68567ac92 100644 --- a/routes/web.php +++ b/routes/web.php @@ -16,7 +16,6 @@ // include dots and slashes as well ->where('slug', '.*'); - Route::redirect('/rss.xml', '/rss'); Route::get('/rss', RssController::class); @@ -33,7 +32,6 @@ Route::redirect('/about', '/'); Route::redirect('/blog', '/#posts'); - Route::redirect('/book', 'https://leanpub.com/rector-the-power-of-automated-refactoring'); Route::redirect('/book/the-power-of-automated-refactoring', 'https://leanpub.com/rector-the-power-of-automated-refactoring'); Route::redirect('/book-detail/rector-the-power-of-automated-refactoring', 'https://leanpub.com/rector-the-power-of-automated-refactoring');