I would like to change the routing a bit. I create a separate virtual page for each user. This is accessible under /profile/USERID. However, I would like to make this accessible under /profile/USERNAME/USERID/.
However, if the user changes the user name, the same profile should still be accessed.
Possible urls can be, if the user change his name:
/profil/tim/14KHeYF5
/profil/timhartmann/14KHeYF5
There should be no difference, because the same page is called up on the basis of the ID without throwing an error.
Do you have an idea how to solve this? I am at a loss after many attempts.
models/profile.php
<?php
class ProfilePage extends Page
{
public function children()
{
$usersPages = [];
$users = kirby()->users();
$currentUser = kirby()->auth()->currentUserFromSession();
$isCurrentUser;
foreach ($users as $key => $user) {
if($currentUser == $user->id()) {
$isCurrentUser = true;
} else {
$isCurrentUser = false;
}
$userPages[] = [
'slug' => Str::slug($user->id()), // or username if unique
'num' => $user->indexOf($users),
'template' => 'profile',
'model' => 'profile',
'content' => [
'title' => $user->username(),
'user' => $user,
'currentUser' => $currentUser,
'isCurrentUser' => $isCurrentUser,
'position' => $user->position(),
'bio' => $user->bio()
]
];
}
return Pages::factory($userPages, $this);
}
}