Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Executing a class method from the URL (for logout method)


Go to End


13 Posts   3842 Views

Avatar
Xurk

Community Member, 50 Posts

16 June 2010 at 10:33pm

Edited: 16/06/2010 10:34pm

I've been searching for a solution for a while on the forums and on Google, but haven't managed to find one. There are some threads on the forums which cover a custom "logout" method for a logged in Security Group member, but the responses given there unfortunately don't help either.

My problem is that I have created a Security Group named "Professionals" and have the logging in part functioning. After logging in, I want to provide them with a "Log out" link which is visible on every page of the website. I looked at the Forum module (and the ForumMemberProfile class to be specific) to see how it is done there, as the module also supplies a "Log out" hyperlink.
This hyperlink points towards "ForumMemberProfile/logout".

In an attempt to mimic this functionality, I set my hyperlink to "Professionals/logout", but when it is clicked on, I receive a "Page not found" error. This seems pretty logical as the page doesn't exist, but I can't seem to figure out how Forum does this? I noticed that the class has the $URLSegment variable set to "ForumMemberProfile" and tried that with my class, but that also doesn't do the trick.

Another thread on the Forums suggested putting a "logout" method in the Page_Controller class and then a hyperlink in the template pointing to "home/logout" (the same principle). So I moved my method into the Page_Controller class in Page.php and changed my hyperlink to "{$BaseHref}logout". But this also gives me a "Page not found" error.

Can anyone explain to me how exactly calling classes and their functions from the page URL works? Any help would be greatly appreciated :)

Avatar
swaiba

Forum Moderator, 1899 Posts

17 June 2010 at 12:31am

Hi,

I am not sure I follow... can you not just link to... www.yoursite.com/Security/Logout?

Barry

Avatar
Xurk

Community Member, 50 Posts

17 June 2010 at 12:58am

Hi Barry,

Thanks for your reply. I've just tried "Security/Logout" and this does indeed do the trick :) Thanks for the tip!

One issue I still have though: it appears that this uses "Director::redirectBack()" after clearing the session variables. While this isn't that bad, I'd rather redirect the user to the log-in form after successfully being logged out - which is what I have in my custom "logout()" method.

My belief that this should be possible is fueled by how the "Forum" module does things and by this post by Willr. So while the "Security/logout" solution is fine for now, if anyone has any further advice, I'd love to hear it :)

Avatar
Willr

Forum Moderator, 5523 Posts

17 June 2010 at 5:18pm

You can do this, I think I mixed up logout (since its not a static) try something like

//mysite/code/Page.php -> Page_Controller 

function logout() {
		if($member = Member::currentUser()) {
			$member->logOut();	
				return Director::redirect('something');
		}
}

Then your logout url like site.com/home/logout/

Avatar
Xurk

Community Member, 50 Posts

17 June 2010 at 7:05pm

Thanks for the code snippet, Willr. Funny, that's almost exactly the same code as I already had in my Page_Controller:

function logout() {
			if ($oProfessional = Member::currentUser()) {
				$oProfessional->logOut();
			}

			return Director::redirect("netwerk/inloggen/");
		}

Though for some reason, navigating to "home/logout" just provides me with the "Page not found" template :(

Avatar
Willr

Forum Moderator, 5523 Posts

17 June 2010 at 7:08pm

You may have to add 'logout' as an allowed action.

In your page controller you need an array like

static $allowed_actions = array(
'logout'
);

If that array already exists just add 'logout' to it.

Avatar
Xurk

Community Member, 50 Posts

17 June 2010 at 7:41pm

Bingo! That one hit the jackpot - thanks a million :)

As always, it turns out to be something glaringly obvious, once you find out that it exists...!

Avatar
cuSSter

Community Member, 56 Posts

18 August 2010 at 8:20am

Hi!

I'm trying to do the same thing, but instead of adding extra code to the Page_Controller, I used a decorator class.
But whenever I click on the logout link, I get a page-not-found error. What did I miss?

Go to Top