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