Broken Logout Link in 5.7

[EDIT] I managed to solve this question myself but have left this here for anyone else's reference as I spent hours trailing the internet for an answer, but could never find one! Eventually found the core file where the code was I needed.

In order to display a logout button on the bottom of my theme I include the following piece of code:

<a href="<?= View::url('/login','logout'); ?>">Logout</a>

This produces the URL "/login/logout", however when visited, it fails to log out the user, and instead just shows the login page - but when the back button is clicked (and page refreshed multiple times to check for cache) the user is still logged in.

Looking at the admin side-bar logout button, I found the URL is different:

It seems to have some session ID attached, and I'm wondering if this is why my link fails to log out the user?

Any help would be great!

Thanks :)

View Replies: View Best Answer
whollands replied on at Permalink Best Answer Reply
The following code gets the user's login session and logs the user out successfully:

<a href="<?php echo URL::to('/login', 'logout', Loader::helper('validation/token')->generate('logout'))?>"><?php echo t('Sign Out.')?></a>
goodnightfirefly replied on at Permalink Reply
FYI there is an inbuilt method as of 5.7.4 which essentially does exactly what you said

<?php echo Core::make('helper/navigation')->getLogInOutLink(); ?>
whollands replied on at Permalink Reply 1 Attachment
That would be a lot better than the following code I have, but I just get an error (see attachment) when using that code, any idea why?
goodnightfirefly replied on at Permalink Reply
Your site is still on version 5.7.2, as I mentioned you would need 5.7.4+ as it was introduced in that version.

As of right now though, 5.7.4 hasn't officially been released, but it will be very soon.

Sorry for the confusion.
whollands replied on at Permalink Reply
Ah thanks - didn't see that! Will update my code when upgrading, thanks for the advice