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.

General Questions /

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

Session::set() not saving data across controllers


Reply


3 Posts   361 Views

Avatar
Double-A-Ron

Community Member, 604 Posts

5 February 2014 at 5:04pm

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

Avatar
simon_w

Forum Moderator, 474 Posts

5 February 2014 at 5:45pm

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().

Avatar
Double-A-Ron

Community Member, 604 Posts

10 February 2014 at 9:59am

Derp.

Thanks Simon. That fixed it.