I’m writing a module for a (very custom) multi-step membership application. I’ve got template group “apply”, and then apply/step1, apply/step2, etc. In those templates are tags mapping directly to the core module’s functions. Well, I might as well post the code 😊
‘apply’ (the index template) looks like
{exp:apply:index}
{if error}
Sorry, you cannot access this application for the following reason:
{error}
{if:else}
You can continue the application.
{/if}
{/exp:apply:index}and the corresponding function in the core module:
function index() {
$tagdata = $this->EE->TMPL->tagdata;
$error = $this->EE->session->flashdata('error_message');
$tags[] = array(
'error' => (isset($error) ? $error : '')
);
return $this->EE->TMPL->parse_variables($tagdata, $tags);
}Then the template ‘apply/step1’ looks like
{exp:apply:step1}
<!-- form stuff here -->
{/exp:apply:step1}And in the core module:
function step1()
{
// eventually will do a lot more to make sure they can access step 1
if ($this->EE->session->userdata['group_id'] != 5)
{
$this->EE->session->set_flashdata('error_message', 'You must be logged in.');
$this->EE->functions->redirect($this->EE->functions->create_url('apply'));
}
return '';
}Basically, at each step I need to do a bunch of checks to verify that the user can access that step, and if not, throw them back to /apply with the error message. I thought flashdata would be the right tool for the job…
But whenever I do a redirect with flashdata, ‘&fld=y’ gets added to the URL, causing a 404. So instead of redirecting to /apply it goes to /apply&fld=y . Odd, since I thought flashdata was cookie-based?
EDIT: Build is 20091207
Hi Brandon- Can you try changing this line:
$this->EE->functions->redirect($this->EE->functions->create_url('apply'));to:
$this->EE->functions->redirect($this->EE->functions->create_url('apply', true));Notice the true boolean. This will set “trailing_slash” to true. This should produce a url like /apply/&fld=y which may solve the 404.
Here is the Docs page for the create_url() function.
Does this solve your issue?
Adam, no go unfortunately. In my experiences with CodeIgniter, flash data didn’t affect the URI…
To simplify, I got rid of the conditional test for member group in step1(). So all step1() does is set flash data and then redirect. Here’s what I tried/found:
OK - is this forum still alive though? It’s not searchable via Advanced Search any longer. I thought this was for beta2, and I’m on the PB (non-commercial as this is for a 501c).
Something else I’ve just tried is using $this->EE->session->cache instead of flashdata. However the cache array doesn’t persist across page loads (whether it’s a redirect or not), which seems seriously wrong.
Is it possible that the cache and flashdata are only meant for use on the control panel side, and not the front-end? If so the manual is pretty misleading… If that is indeed the case, I’d like to know another way to pass data to a template following a redirect (other than using the DB, which seems a bit overkill).
Thanks Ingmar and everyone else for their assistance.
Not to be a pain (too late, I know! :lol:) but the same exact thing happens using PHP in regular templates. Example:
template ‘apply/test1’:
<?php
$this->EE->session->set_flashdata('test_message', 'Flashdata test');
$this->EE->functions->redirect($this->EE->functions->create_url('apply/test2'));
?>template ‘apply/test2’:
<?php
$test_message = $this->EE->session->flashdata('test_message');
echo $test_message;
?>Viewing test1 redirects to:
http://test.local/index.php/apply/test2&fld=ySo this isn’t really module related… I guess I’d just like an ‘official’ answer on whether this is expected behavior in 2.0PB (1.x didn’t have flashdata since it wasn’t built on CI). Thanks!
And here’s a similar test for the session cache:
template ‘test1’
<?php
$this->EE->session->cache['test']['message'] = 'hello';
// confirms values are set...
var_dump($this->EE->session->cache);
?>template ‘test2’
<?php
// shows as empty!
var_dump($this->EE->session->cache);
?>View test1 and then test2… the cache shows as empty. This should definitely work on the front end from what I can tell.
EDIT: Ah, re-re-reading the manual - session->cache is for “values that you would like to persist during a page load” (emphasis on during - won’t work between pages).
Then I fall back to the flashdata question, as to whether it is usable on the frontend? The user guide is ambiguous here…
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.