Skip to content

Commit

Permalink
[routes] add alias for short urls
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 29, 2024
1 parent 127d7d9 commit ab1c2a6
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 8 deletions.
11 changes: 6 additions & 5 deletions app/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(
private string $content,
private ?DateTimeInterface $updatedAt,
private ?string $updatedMessage,
private ?string $alias,
) {
}

Expand Down Expand Up @@ -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;
Expand All @@ -95,4 +91,9 @@ public function hasTweets(): bool
{
return str_contains($this->content, 'class="twitter-tweet"');
}

public function getAlias(): ?string
{
return $this->alias;
}
}
2 changes: 2 additions & 0 deletions app/EntityFactory/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public function createFromFilePath(string $filePath): Post
$matches['content'],
$updatedAt,
$configuration['updated_message'] ?? null,
$configuration['alias'] ?? null,

);
}
}
7 changes: 6 additions & 1 deletion app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand Down
4 changes: 4 additions & 0 deletions app/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

---

Expand Down
2 changes: 0 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// include dots and slashes as well
->where('slug', '.*');


Route::redirect('/rss.xml', '/rss');
Route::get('/rss', RssController::class);

Expand All @@ -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');
Expand Down

0 comments on commit ab1c2a6

Please sign in to comment.