21309 Posts in 5738 Topics by 2603 members
General Questions
SilverStripe Forums » General Questions » Executing a class method from the URL (for logout method)
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
| Go to End | Next > | |
| Author | Topic: | 1622 Views |
-
Executing a class method from the URL (for logout method)

16 June 2010 at 10:33pm Last edited: 16 June 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
-
Re: Executing a class method from the URL (for logout method)

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
-
Re: Executing a class method from the URL (for logout method)

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/
-
Re: Executing a class method from the URL (for logout method)

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
-
Re: Executing a class method from the URL (for logout method)

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.
-
Re: Executing a class method from the URL (for logout method)

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...!
-
Re: Executing a class method from the URL (for logout method)

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?
| 1622 Views | ||
| Go to Top | Next > |




