CakePHP: action is performed twice, without a good reason

I have a strange problem with my pie (cake_1.2.0.7296-rc2). My start () - the action is performed twice under certain circumstances, although only one request is executed.

Triggers look like this: - loading an object like: $this->Questionnaire->read(null, $questionnaire_id); - access to $ this-data p>

If I disconnect the call to loadAvertisement() from start() -action, this will not happen. If I disconnect two calls inside loadAdvertisement():

 $questionnaire = $this->Questionnaire->read(null, $questionnaire_id); $question = $this->Questionnaire->Question->read(null, $question_id); 

... then this will not happen either.

Why?

See my code below, the controller is "questionnaires_controller".

 function checkValidQuestionnaire($id) { $this->layout = 'questionnaire_frontend_layout'; if (!$id) { $id = $this->Session->read('Questionnaire.id'); } if ($id) { $this->data = $this->Questionnaire->read(null, $id); //echo "from ".$questionnaire['Questionnaire']['validFrom']." ".date("ymd"); //echo " - to ".$questionnaire['Questionnaire']['validTo']." ".date("ymd"); if ($this->data['Questionnaire']['isPublished'] != 1 //|| $this->data['Questionnaire']['validTo'] < date("ymd") //|| $this->data['Questionnaire']['validTo'] < date("ymd") ) { $id = 0; $this->flash(__('UngΓΌltiges Quiz. Weiter zum Archiv...', true), array('action'=>'archive')); } } else { $this->flash(__('Invalid Questionnaire', true), array('action'=>'intro')); } return $id; } function start($id = null) { $this->log("start"); $id = $this->checkValidQuestionnaire($id); //$questionnaire = $this->Questionnaire->read(null, $id); $this->set('questionnaire', $this->data); // reset flow-controlling session vars $this->Session->write('Questionnaire',array('id' => $id)); $this->Session->write('Questionnaire'.$id.'currQuestion', null); $this->Session->write('Questionnaire'.$id.'lastAnsweredQuestion', null); $this->Session->write('Questionnaire'.$id.'correctAnswersNum', null); $this->loadAdvertisement($id, 0); $this->Session->write('Questionnaire'.$id.'previewMode', $this->params['named']['preview_mode']); if (!$this->Session->read('Questionnaire'.$id.'previewMode')) { $questionnaire['Questionnaire']['participiantStartCount']++; $this->Questionnaire->save($questionnaire); } } function loadAdvertisement($questionnaire_id, $question_id) { //$questionnaire = array(); $questionnaire = $this->Questionnaire->read(null, $questionnaire_id); //$question = array(); $question = $this->Questionnaire->Question->read(null, $question_id); if (isset($question['Question']['advertisement_id']) && $question['Question']['advertisement_id'] > 0) { $this->set('advertisement', $this->Questionnaire->Question->Advertisement->read(null, $question['Question']['advertisement_id'])); } else if (isset($questionnaire['Questionnaire']['advertisement_id']) && $questionnaire['Questionnaire']['advertisement_id'] > 0) { $this->set('advertisement', $this->Questionnaire->Question->Advertisement->read(null, $questionnaire['Questionnaire']['advertisement_id'])); } } 

I really do not understand this ... he does not think so. Any help would be greatly appreciated! :)

Regards, Stu

+4
source share
9 answers

Check the layout for nonexistent links, for example an improperly configured link to favicon.ico will cause the controller action to run a second time. Make sure favicon.ico points to webroot, not the local directory, otherwise requests will be generated for /controller/action/favicon.ico, not /favicon.ico, and thus trigger your action.

This can also happen with images, style sheets, and javascript.

To meet the check, id $ is int, and then check that $ id exists as the primary key in the database before moving on to any functionality.

+10
source

For me it was a JS issue.

Take care of the wrap function with jQuery that restart JS in wrapped content!

+2
source

You might want to try to figure out where it came from using the debug_print_backtrace () function. ( http://nl.php.net/manual/en/function.debug-print-backtrace.php

+1
source

There was the same problem, with a certain action it was randomly performed 2-3 times. I found two reasons:

  • Add-ons Firefox Yslow has been configured to automatically download from it. Preferences that cause page reloading when using F5 (not when loading a page from the browser address bar and pressing Enter).

  • I had an erroneous CSS style declaration in the parameters of $ html-> link (); in some cases, it will look like background-image: url ('');, which also caused a restart. Setting the style of the link to the background image: no; when the image was not available to me.

Hope this helps. I know this is a rather old post, but when it comes to finding this problem, I think it can help others by continuing to post.

Good luck.

Jeren den Haan

+1
source

I had a problem like last week.

Two possible reasons.

  • Faulty routes (check route settings)
  • Invalid AppController. I am adding a lot of things to the AppController, especially before beforeFilter () and beforeRender (), so you can also check them out.

One more thing: where do you install Questioneer.id in your session? Maybe a problem?

+1
source

Yes, this happens when there is a broken link on the web page. Each browser deals with it differently (Firefox calls it 2x). I tested it, there is no difference in CakePHP v1.3 and v2.2.1. To find out who is the culprit, add this line to the code, and then open the second generated file in the www folder:

 file_put_contents("log-" . date("Hms") . ".txt", $this->params['pass'] ); // CakePHP v1.3 file_put_contents("log-" . date("Hms") . ".txt", $this->request['pass'] ); //CakePHP v2.2.1 

PS: First, I blame jQuery for this. But in the end, the image for loading AJAX in the 3rd part of the script was forgotten.

+1
source

I had the same problem in chrome, I disabled my HTML Validator. That loaded the page twice

0
source

I had a similar problem, the problem seemed to be isolated from case insensitivity on the endpoint.

those.:
http: // server / Questionnaires / loadAvertisement -vs-
http: // server / Questionnaires / loadAvertisement

When the endpoint was called with the proper bypass, the method was run once - in the case when the bottom cover was run twice. The problem arose sporadically - it occurs on one controller, but not on another (essentially the same logic, no additional components, etc.). I could not confirm, but I believe that the browser error is not CakePHP itself.

My workaround was that each link to the endpoint was properly. To go even further, I added general options for route configuration options:

application /Config/routes.php

 <?php // other routes.. $instructions = ['controller'=>'Questionnaires','action'=>'loadAvertisement']; Router::connect('/questionnaires/loadavertisement', $instructions); Router::connect('/QUESTIONNARIES/LOADADVERTISEMENT', $instructions); // ..etc 
0
source

If you skip <something> , such as View, Cake will cause a missing <something> error, and it will try to display its Error View . Therefore, the AppController will be called twice. If you fix the missing problem, AppController is called once.

0
source

Source: https://habr.com/ru/post/1276691/


All Articles