Front Controller (app.php) called multiple times for 1 request

896 Views Asked by At

Here is my issue.

Situation

I am debugging symfony2 application (having some issues with posted data). On this occasion, i checked how many times app.php vs app_dev.php are called when handling a request.

example: request (from browser) : /demo/display/foos

logger in app.php (resp. app_dev.php):

//logger function 
function logtxt($logtxt, $name='myLog'){
  $fp = fopen($name.'.txt','a+');
  fseek($fp,SEEK_END);
  $newLog=date('H:i:s', time()).' - '.':'."\r\n".$logtxt."\r\n".'--------------------------------------------';
  fputs($fp,$newLog);
  fclose($fp); //basta
}

//logger call
logtxt('in app_dev.php ('.$_SERVER['SCRIPT_FILENAME'].')');

it basically adds a line in a logfile each time logtxt is called.

Issue

It appears that, in my applications, app_dev.php is called multiple times for a single request (mostly 2 times or 3 times). I tried with an empty project: app.php or app_dev.php are called only on time at each request.

I was thinking of redirections I would be doing, but this is not always the case. More amazing, there is sometimes a difference between app.php log count and app_dev.php log count for a same request!

I do not understand why the front-controller should be called more than once in a request. Also, it seems that it has some impact in production. For instance, in an old project, $_POST gets reset between these calls and its data seem to get lost (!).

for instance, for 1 same request, I will have in dev and prod envs:

15:45:51 - in app_dev.php:
in app_dev.php ($post: {"sort":"sort","property":"barcode"})

15:45:51 - in foo Controller:
fooControllerAction called ($post: {"sort":"sort","property":"barcode"})
--------------------------------------------
15:45:52 - in app_dev.php:
in app_dev.php ($post: [])
--------------------------------------------

whereas with app.php log:

15:44:07 - in app.php:
in app.php ($post: {"sort":"sort","property":"barcode"})

--------------------------------------------
15:44:07 - in app.php:
in app.php ($post: [])
--------------------------------------------
15:44:07 - in fooController:
fooControllerAction called ($post: [])
--------------------------------------------

Consequence: posted variable are usable by controller in dev environment, but not in prod environment, for the same request:

Solution

Is this behaviour known to someone ? I am eager to understand background justifying this, and check that it doesn't lead to errors. Also, if this is due to bad dev practice, I would change mine.

Thanks a lot in advance for sharing your wisdom!

Cheers,

1

There are 1 best solutions below

4
On BEST ANSWER

For setting kernel listener see this page:

http://symfony.com/doc/current/cookbook/service_container/event_listener.html

... specifically this section which covers request listener:

http://symfony.com/doc/current/cookbook/service_container/event_listener.html#request-events-checking-types

You have here an example on how to detect if request is subrequest or master ;)

When you have detected HttpKernel::MASTER_REQUEST as your request type you could apply the same logic you have shown in your question...