Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Session::set() not saving data across controllers

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
Go to End
Author Topic: 323 Views
  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Session::set() not saving data across controllers Link to this post

    I have an interesting issue here with Silverstripe's Session object. The following is for a frontend login form:

    1. User logs in using form
    2. Submit processes on an action in the same controller as the form above. This controller extends Page_Controller.
    3. The login process is a call to a method in a class that extends nothing. APIManager. This in turn makes an API call to check creds.
    4. Sometimes, we are throwing custom exceptions. One of those is APICommuncationException which extends Exception. This is what is happening in this case.
    5. This Exception sets a Session param: Session::set('SiteNotice', "API unavailable");
    6. The exception then calls a method called logoutAndRedirect() which is in a class that extends Extension and is tied to Controller in config.yml. This is where it gets messy. As the only way I have found to access this from the Exception class is this: Controller::curr()->logoutAndRedirect(); (which works)
    7. This is the contents of logoutAndRedirect:

    public function logoutAndRedirect() {
          
          // Gets the current page object
          $owner = $this->owner;
          
          // Default user message
          if(!Session::get('SiteNotice')) {;
             Session::set('SiteNotice', "this is a test");
          }
       
          // Redirect home including the return url
          $owner->redirect(Director::absoluteBaseURL() . "access/login/?redirect=" . $owner->Link());
          $owner->response->output();
          exit();
       }

    The last lines of that function redirect the user to the same login page described in step 1. above. What I have found is that when I dump Session::get_all() at the top, even if it's the first thing on init(), the SiteNotice element is not there. I've added some other bogus elements and the same occurs. I have also put break points on all the clear methods within the Session class and they are not being hit.

    There is this line in the doc (http://api.silverstripe.org/master/class-Session.html):
    "In order to support things like testing, the session is associated with a particular Controller."

    I'm wondering if that has something to do with it? Can anyone see a problem with what I'm trying to do here?

    Chur
    Aaron

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Session::set() not saving data across controllers Link to this post

    The problem with this is you're calling exit() without saving the session. As you're bypassing the normal flow of this, you need to call Session::save(); before exit().

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Session::set() not saving data across controllers Link to this post

    Derp.

    Thanks Simon. That fixed it.

    323 Views
Page: 1
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.