I don't think there's really any way to handle this when the user browses *away* from some page but you could certainly remove the session key for any action *except* this one. Put some code in AppController::beforeFilter() that checks the controller/action and unsets the key if it isn't this one.
That doesn't answer the question whether this is the best way to be doing this in the first place, though. On Wed, Jul 1, 2009 at 12:12 PM, DavidH<[email protected]> wrote: > > Hi All > > I've a question about how to delete a session variable when the user > navigates away from a page. > > I use a session variable to persist an id that the user has selected > from a drop down list. This id is then used as find conditions in > subsequent operation. > > It works like a dream; but when the user navigates away from this page > to another part of the application, and then comes back some time > later, the variable is still present and so I get the "special case" > behaviour instead of the default behaviour. > > I could put a session->delete in every other controller; but that's > not very robust as I'm bound to forget when creating new controllers. > Does anyone know of a procedure for deleting session variables when > navigating away from a page? > > Or maybe I should be using session variables for this at all. > > Here's some code from my controller index method showing the session > variable being set or read: > > function index() { > > $displaykpi = null; // The specific kpi to display the > levels for > > // > // If something was passed to this method then it must be a > request to > // display a single KPI rather than the entire list > // > if (!empty($this->data)) > { > $displaykpi = array('kpi_id' => $this->data['KpiLevels'] > ['kpi_id']); > > // > // Store the KPI in the session and set up pagination to > only > // display the KPI we've selected. > // > $this->Session->write('displaykpi', $displaykpi); > } > else > { > // > // Check is something is set in the session > // > if ($this->Session->check('displaykpi')) > { > $displaykpi = $this->Session->read('displaykpi'); ## > We only want to do this if user hasn't been > > ## somewhere else in the app. > } > > } > > // > // If we're working on a specific KPI only paginate that KPI > // otherwise paginate all of them > // > if ($displaykpi) > { > $kpiLevels = $this->paginate($displaykpi); > } > else > { > $kpiLevels = $this->paginate(); > } > // > // Get all the KPI's so we can give the user the option to > choose just > // one KPI to display > // > $allkpis = $this->KpiLevel->Kpi->find('list'); > > $this->set('kpiLevels', compact('kpiLevels', 'allkpis')); > > } > > Here's my index.ctp (part of) showing the form that's used to pass the > kpi_id back to index.ctp > > <?php > $javascript->codeBlock('function getKPI(f) {f.form.submit(); } ', > array('inline' => false)); > ?> > <div class="kpiLevels index"> > <h2><?php __('KpiLevels');?></h2> > <div class="kpilevels form"> > <?php > // > // Create a form to allow the user to select which specific > KPI to display > // > $allkpis = $kpiLevels['allkpis']; > echo $form->create('KpiLevels', array('action' => 'index')); > echo $form->label('kpi_id', 'Select a KPI to display'); > echo $form->select('kpi_id', $kpiLevels['allkpis'], null, array > ('onChange' => 'getKPI(this)')); > echo $form->end(); > ?> > </div> > > The rest of the index.ctp is box standard pagination code. > > Hope someone can help > > Regards > > David > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
