RSS

Neil Crookes

Learnings and Teachings on Web Application Development & CakePHP

Jan

29

Send ‘em back from whence they came

A CakePHP component for tracking a user’s journey through your app, and redirecting them back to previous pages instead of hard coding the address. Code is ribbed… I mean MIT licensed for your pleasure.

Share and Enjoy:

  • Digg
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Slashdot

First off, this is not a post about Nationalism. I’m not anti-immigration.

Right, that’s settled, so, what is it about? Well it’s about a CakePHP component I’ve written for remembering where a user has been on your site, so after they’ve completed an action, you can send them back to where they’ve come from, rather than hard-coding where they get redirected to.

Although the main application for this code is going to be in an admin area of your app, sending the user back to an index action after completing an CRUD action, and CakePHP’s baked controllers do this for you already, the index action they go back to will be the default recordset e.g. records 1 to 20, ordered by ID.

If your user has sorted the list by another field and navigated to page 23 out of 37, or perhaps you’ve got filtering capability in your app (see future post), where the filter params are in the URI, having to get back to this point in the list is gonna piss them off a treat!

Enter the HistoryComponent.

The History Component manages a stack or First In, Last Out (FILO) buffer, in the user’s session.

There are separate stacks for the actual front end of a site and the admin (as defined by Routing.admin config) parts of an application, so if you’ve got 2 tabs open in your browser one for the front end and one for the admin section, you don’t get redirected to URIs in the wrong part of the app.

The stack entries include the REQUEST_URI server variable and the Controller::pageTitle values so you can add “Back to <previous page title>” links in your views. It also automatically sets a view variable with the title and URI of previous page for you.

The stacks are automatically updated in the components beforeRender callback (beforeRender is used instead of beforeFilter because Controller::pageTitle has been set by then whereas it hasn’t by the time beforeFilter is called), so integrating it into your application is as easy as including it in your controller’s components array.

class AppController extends Controller {
  var $components = array('History');
}

There is a back() method which can redirect to any of the previous pages. In a typical app you might have an index action from where you can edit a record – after saving the record you want to go back to the index action, which is at position 1 in the stack, or using the JavaScript history metaphor, -1 (this is the default stack item the user gets redirected to). Whereas after deleting a record you want to go back to the index but in this case it’s at position 0 because there was no render from the delete action.

function edit($id = null) {
  //edit code
  $this->History->back();
}

function delete() {
  // delete code
  $this->History->back(0);
}

Back() can also accept a default if likely to be used in an action where there may not be a previous page in the session stack, for example if the user came from another site.

So that’s the HistoryComponent in a nutshell. The code is MIT Licensed, so feel free to grab it from my github account, improve it extend it etc.

Share and Enjoy:

  • Digg
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Slashdot
(3 votes, average: 3.33 out of 5)
Loading ... Loading ...

Comments are closed.