Welcome Guest, Not a member yet? Register   Sign In
Shutdown hooks and redirect()
#1

[eluser]Unknown[/eluser]
First post here... hi everyone.

I've written some simple convenience functions in my parent controller to deal with groups of flashdata messages. I've been using the "post_controller" hook to save this collated data to the session before execution ends. However, I've found that this doesn't work when redirect() has been used, as redirect() halts execution, and therefore none of the hooks in the shutdown sequence get triggered.

At first I did some messing about to see if I could trigger the save in the controller's destructor. This won't work though as the database driver is a member of the controller itself, and so it's already been destroyed by the time the controller's own destructor is called.

So failing that, I've made an edit to the redirect() function, to call the hook I need manually:

Code:
if ( ! function_exists('redirect'))
{
function redirect($uri = '', $method = 'location', $http_response_code = 302)
{
  if ( ! preg_match('#^https?://#i', $uri))
  {
   $uri = site_url($uri);
  }

  global $EXT;
  $EXT->_call_hook('post_controller');

  switch($method)
  {
   case 'refresh' : header("Refresh:0;url=".$uri);
    break;
   default   : header("Location: ".$uri, TRUE, $http_response_code);
    break;
  }
  exit;
}
}

I'm not a fan of this approach though - it's pretty hacky, and obviously still doesn't guarantee that the other hooks are called.

Is there a better way?




Theme © iAndrew 2016 - Forum software by © MyBB