V9 logout via a custom link

I need to add a link (hard coded) that logs out the current user.

I’ve use this in the past, but doesn’t seem to work for v9:

echo '<li class="log-out"><a href="'. URL::to('/login', 'logout', \Core::make('helper/validation/token')->generate('logout')) .'">Log out</a></li>';

Have you tried

<?php echo Core::make('helper/navigation')->getLogInOutLink() ?>

It uses this function

public function getLogInOutLink()
if (!id(new User())->isLoggedIn()) {
$url = URL::to(‘/login’);
$label = t(‘Log in’);
} else {
$url = URL::to(‘/login’, ‘do_logout’, id(new Token())->generate(‘do_logout’));
$label = t(‘Log out’);
return sprintf(‘%s’, $url, $label);

Thanks, I’ll give that a go