views:

234

answers:

3

I have a controller that, among other things, sends emails. I need to echo a message to the user before the email sending starts (otherwise it looks like the screen is stuck).
So, how do I echo a message which is set in the start of a controller, before I reach the end of the controller, or, should I think in another direction all together?

A: 

Try maybe:

<?php
//...
public function someAction()
{
    echo "Something";
    ob_flush();flush();
}

This forum post discusses your issue. They suggest:

<?php
$frontController = Zend_Controller_Front::getInstance();
$frontController->setParam('disableOutputBuffering', true);

And then performing the ob_flush();flush(); technique.

dcousineau
I need the layout to be there too.
Itay Moav
In that case you could do some kind of AJAX request when your view is loaded, because, the Layout is rendered after the controller Action has been executed, not during.
Chris
Thought so...Thanks!
Itay Moav
A: 

You could try using a shutdown function to send the email. If you also flush the output buffer this will make sure the user gets to see the rendered page first. In your code call:

register_shutdown_function('send_email', $params);

And then have a function that looks like:

function send_email($params) {
    ob_flush();flush();
    // Send your email here
}
Neel
+1  A: 

For the mail part of the question.

Depending on the timing - how soon after the request the message needs to be sent - another possiblity would be to create a message queue (maybe a db table), write a record to that table, and then run a cron process that consumes the queue, sending any unsent messages, marking them as sent, etc.

David Weinraub