我遇到了CodeIgniter控制器被調用兩次的問題。它似乎只發生在我在uri中使用參數時(/ newsletter/confirm/a1938cas893vf9384f0384f0943)。如果我從我的函數中刪除參數,它只會加載控制器一次。我還注意到,使用url中的參數,如果我刷新頁面,它只加載一次。所以它似乎只在調用一個新頁面時加載兩次。CodeIgniter控制器在URL中使用參數時加載兩次
例如,第一次導航到/ newsletter/confirm/a123會導致它加載兩次。但如果你刷新/通訊/確認/ a123它只會加載一次。我完成了對我的觀點的註釋,以消除導致它的視圖的問題。
這聽起來像緩存問題,或者在我的.htaccess文件中?感謝您的任何建議。
相關負責人:
<?php
error_reporting(-1);
ini_set('display_errors',1);
class Test extends CI_Controller {
function __construct() {
parent::__construct();
log_message('debug', 'MyController initialised');
}
function confirm($code)
{
$this->load->helper(array('form'));
//$code = "6e930fe882c3b15712158812769dbcb636f96b8c";
$result = $this->db->get_where('newsletter_members', array('nm_confirmation_code' => $code, 'nm_subscribed' => 0));
if ($result->num_rows == 0)
{
$newsletter_message['newsletter_message'] = "Confirmation code is invalid or has already been confirmed.";
//$this->load->view('index_test', $newsletter_message);
} else {
$newsletter_message['newsletter_message'] = "Thank you for confirming your intent to subscribe to our newsletter!";
$data = array(
'nm_subscribed' => 1,
);
$this->db->where('nm_confirmation_code', $code);
$this->db->update('newsletter_members', $data);
//$this->load->view('index_test', $newsletter_message);
}
}
}
?>
.htaccess文件:
RewriteEngine On
RewriteCond $1 !^([^\..]+\.php|robot\.txt|public|images|css|js|paul|event_docs|blog|citeforme|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
# BEGIN WordPress
#<IfModule mod_rewrite.c>
#RewriteEngine On
#RewriteBase/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule . /index.php [L]
#</IfModule>
#RewriteEngine Off
# END WordPress
下面是日誌文件的樣子,你可以看到一切都被重新加載兩次:
DEBUG - 2011-09-16 09:59:34 --> Config Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Hooks Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Utf8 Class Initialized
DEBUG - 2011-09-16 09:59:34 --> UTF-8 Support Enabled
DEBUG - 2011-09-16 09:59:34 --> URI Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Router Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Output Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Input Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Global POST and COOKIE data sanitized
DEBUG - 2011-09-16 09:59:34 --> Language Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Loader Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Database Driver Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Controller Class Initialized
DEBUG - 2011-09-16 09:59:34 --> MyController initialised
DEBUG - 2011-09-16 09:59:34 --> Helper loaded: form_helper
DEBUG - 2011-09-16 09:59:34 --> Final output sent to browser
DEBUG - 2011-09-16 09:59:34 --> Total execution time: 0.0223
DEBUG - 2011-09-16 09:59:34 --> Config Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Hooks Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Utf8 Class Initialized
DEBUG - 2011-09-16 09:59:34 --> UTF-8 Support Enabled
DEBUG - 2011-09-16 09:59:34 --> URI Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Router Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Output Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Input Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Global POST and COOKIE data sanitized
DEBUG - 2011-09-16 09:59:34 --> Language Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Loader Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Database Driver Class Initialized
DEBUG - 2011-09-16 09:59:34 --> Controller Class Initialized
DEBUG - 2011-09-16 09:59:34 --> MyController initialised
DEBUG - 2011-09-16 09:59:34 --> Helper loaded: form_helper
DEBUG - 2011-09-16 09:59:34 --> Final output sent to browser
DEBUG - 2011-09-16 09:59:34 --> Total execution time: 0.0213
您將消息記錄到某處。控制器父類在其__construct函數中的作用相同,因此在錯誤日誌中,您會看到兩條說明「我的控制器初始化」的錯誤消息和兩條說明「控制器類已初始化」的錯誤消息 - 是的? –
是的,Calle,但這些不是唯一被加載兩次的東西,所有內容都被加載兩次併發送到瀏覽器。當我加載兩次時,我的日誌看起來像我的原始帖子。 – aberrant