2012-03-12 56 views
0

我遇到此路由的問題,不確定我的問題究竟是什麼。CI路由問題無法獲得變量

我的頁面位於http://www.kansasoutlawwrestling.com/kowmanager/pmsystem/viewmessage/1其中1是消息ID。

我成立了一個路線,看起來像

$route['pmsystem/viewmessage/(:num)'] = 'pmsystem/viewmessage/$1'; 

,我仍然得到一個錯誤消息像這樣

A PHP Error was encountered 

Severity: Warning 

Message: Missing argument 1 for Pmsystem::viewmessage() 

Filename: controllers/pmsystem.php 

Line Number: 76 


// View A Message 
function viewmessage($message_id) 
{ 
    //Config Defaults Start 
    $msgBoxMsgs = array();//msgType = dl, info, warn, note, msg 
    $cssPageAddons = '';//If you have extra CSS for this view append it here 
    $jsPageAddons = '<script src='.base_url().'../assets/js/cpanel/personalmessages.js></script><script src='.base_url().'assets/js/mylibs/jwysiwyg/jquery.wysiwyg.js></script>';//If you have extra JS for this view append it here 
    $metaAddons = '';//Sometimes there is a need for additional Meta Data such in the case of Facebook addon's 
    $siteTitle = '';//alter only if you need something other than the default for this view. 
    //Config Defaults Start 


    //examples of how to use the message box system (css not included). 
    //$msgBoxMsgs[] = array('msgType' => 'dl', 'theMsg' => 'This is a Blank Message Box...'); 

    /**********************************************************Your Coding Logic Here, Start*/ 

    // Checks to see if a session is active for user and shows corresponding view page 
    if (!$this->loggedin->chkLoginStatus() === FALSE) 
    { 
     if(! $this->uri->segment(3)) 
     { 
      redirect('error', 'refresh'); 
     } 
    } 
    else 
    { 
     redirect('login', 'refresh'); 
    } 
    $bodyContent = 'viewpm';//which view file 
    $bodyType = "full";//type of template     

    /***********************************************************Your Coding Logic Here, End*/ 

    //Double checks if any default variables have been changed, Start. 
    //If msgBoxMsgs array has anything in it, if so displays it in view, else does nothing.  
    if(count($msgBoxMsgs) !== 0) 
    { 
     $msgBoxes = $this->msgboxes->buildMsgBoxesOutput(array('display' => 'show', 'msgs' =>$msgBoxMsgs)); 
    } 
    else 
    { 
     $msgBoxes = array('display' => 'none'); 
    } 

    if($siteTitle == '') 
    { 
     $siteTitle = $this->metatags->SiteTitle(); //reads 
    } 

    //Double checks if any default variables have been changed, End. 

    $this->data['msgBoxes'] = $msgBoxes; 
    $this->data['cssPageAddons'] = $cssPageAddons;//if there is any additional CSS to add from above Variable this will send it to the view. 
    $this->data['jsPageAddons'] = $jsPageAddons;//if there is any addictional JS to add from the above variable this will send it to the view. 
    $this->data['metaAddons'] = $metaAddons;//if there is any addictional meta data to add from the above variable this will send it to the view. 
    $this->data['pageMetaTags'] = $this->metatags->MetaTags();//defaults can be changed via models/metatags.php 
    $this->data['siteTitle'] = $siteTitle;//defaults can be changed via models/metatags.php 
    $this->data['bodyType'] = $bodyType; 
    $this->data['bodyContent'] = $bodyContent; 
    $this->data['user_data'] = $this->users->getUserByUserId($this->session->userdata('user_id')); 
    $this->data['users'] = $this->loggedin->getUserList(); 
    $this->data['personal_messages'] = array($this->pmmodel->getTotalMessages($this->session->userdata('user_id')), $this->pmmodel->getTotalUnreadMessages($this->session->userdata('user_id')), $this->pmmodel->getLast5Messages($this->session->userdata('user_id'))); 
    $this->data['messages'] = array($this->pmmodel->getInboxMessages($this->session->userdata('user_id')), $this->pmmodel->getSentMessages($this->session->userdata('user_id'))); 
    //$this->data['message_data'] = $this->pmmodel->getPmMessage($this->uri->segment(3)); 
    $this->load->view('cpanel/index', $this->data); 
} 

UPDATE

// Checks to see if a session is active for user and shows corresponding view page 
    if (!$this->loggedin->chkLoginStatus() === FALSE) 
    { 
     if (!is_numeric($this->uri->segment(3))) 
     { 
      $this->data['message_data'] = 'Invalid message id!'; 
     } 
     else 
     { 
      $this->data['message_data'] = $this->pmmodel->getPmMessage($this->uri->segment(3)); 
     } 
     $bodyContent = 'viewpm';//which view file 
    } 
    else 
    { 
     redirect('login', 'refresh'); 
    } 

    $bodyType = "full";//type of template 
+0

你的控制器是什麼樣的,特別是'viewmessage'函數?順便說一下,你的路線是多餘的 - 它不會改變什麼,你期望它做什麼,默認情況下還沒有完成? – 2012-03-12 20:28:11

+0

我更新了我的代碼。感謝您的回覆。當沒有id集時,我希望它被重定向到錯誤控制器。 – 2012-03-12 20:30:48

回答

1

這條路線是不必要的 - 它不會改變任何東西。

$route['pmsystem/viewmessage/(:num)'] = 'pmsystem/viewmessage/$1'; 

您可以刪除該路線。問題在於:

function viewmessage($message_id) // no default value means it's required 
{ 
    // your code 
} 

您的控制器方法從字面上接受用戶輸入作爲參數(無論在地址欄中)。您始終必須考慮那些在CI控制器方法中不存在的必需參數。

function viewmessage($message_id = NULL) 
{ 
    if (! $message_id) show_404(); 
    // your code 
} 

如果所需的$message_id不存在,這將消除錯誤並顯示404。另外,$this->uri->segment(3)是不必要的,因爲它應該具有與$message_id相同的值。

我非常不喜歡重定向到一個錯誤頁面,當你真的想要一個404,但這取決於你。這肯定不會幫助用戶在重定向後地址丟失時意識到他們的錯誤,並且通過這樣做發送錯誤的HTTP標頭。

+0

好的,謝謝你的幫助 – 2012-03-12 20:39:52

+0

我修正了,但是我想要做的是找出它的數字,如果沒有,然後讓它發送消息給沒有指定消息的視圖。我有我的代碼上面更新。我想知道這是否足夠好用,以及如果我應該如何處理這樣一個事實,即如果某人不是發件人或收件人嘗試訪問該郵件的人。我應該在什麼時候阻止他們訪問該消息。 – 2012-03-12 21:06:52

+0

只需將它傳遞給數據庫並查看是否得到結果。沒有任何其他方式驗證它的意義。 – 2012-03-12 21:09:34