Jump to:

22972 Posts in 11596 Topics by 2824 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, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 2044 Views
  • Xurk
    Avatar
    Community Member
    50 Posts

    Executing a class method from the URL (for logout method) Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Executing a class method from the URL (for logout method) Link to this post

    Hi,

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

    Barry

  • Xurk
    Avatar
    Community Member
    50 Posts

    Re: Executing a class method from the URL (for logout method) Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Executing a class method from the URL (for logout method) Link to this post

    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/

  • Xurk
    Avatar
    Community Member
    50 Posts

    Re: Executing a class method from the URL (for logout method) Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Executing a class method from the URL (for logout method) Link to this post

    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.

  • Xurk
    Avatar
    Community Member
    50 Posts

    Re: Executing a class method from the URL (for logout method) Link to this post

    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...!

  • cuSSter
    Avatar
    Community Member
    56 Posts

    Re: Executing a class method from the URL (for logout method) Link to this post

    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?

    2044 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.