tags:

views:

215

answers:

3

Hi,

in cakephp is it possible to continue to execute a function after the render call? Im using ajax and it would be nice to be able to do some cleaning up server side after render the response to the page. Of course I could make another ajax call but I would prefer not to..

Tnx for any ideas. Bjorsa

A: 

You could log to a file that stores batch jobs, and then use a cronjob to execute CakePHP's shells (which are executable scripts that have access to the framework). So when you process the Ajax request, log the batch process, and then schedule a crontask to process on a set interval.

CakePHP book's page on shells

If you have a decent host, like Dreamhost, you can easily schedule cron jobs, and will find steps do to so in the host's documentation.

adam
+1  A: 

From the CakePHP docs (emphasis mine):

The render() method is automatically called at the end of each requested controller action. This method performs all the view logic (using the data you’ve given in using the set() method), places the view inside its layout and serves it back to the end user.

But, if you look at the source for AppController::render, it returns the rendered output back to the calling method. So, theoretically, you could do something like:

$this->autoRender = false;
$outp = $this->render('myView');
// do cleanup stuff
echo $outp;
exit();

As long as you have autoRender set to false, you should be good. I've not personally tried this, but it seems like it should work like you want. Good luck!

inkedmn
A: 

Depending on what exactly you want to do you should probably find a better way to do it. Having said that, that's exactly what the Controller::afterFilter() callback is for.

deceze