2012-02-06 61 views
0

我想弄清楚如何處理我的路線和控制器。多個變量後控制器在URL

在用戶在我的網站上註冊後,他們會收到一封電子郵件,其中包含激活控制器的鏈接,要求輸入密碼以確認帳戶。不過,我試圖添加一個if語句到我的控制器,以便它驗證在URL中激活結束時有兩個參數,如果沒有,那麼它將顯示我的錯誤頁面。還需要驗證第一個參數是數字。

由於某種原因,我有一些東西設置正確,不知道是什麼。

這是我使用的路線

$route['activate/:num/:any'] = "activate"; 

控制器:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Activate extends CI_Controller 
{ 

public function __construct() 
{ 
    parent::__construct(); 
    $this->load->library('kow_auth');    
} 

public function index() 
{ 
    //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="http://www.kansasoutlawwrestling.com/kowmanager/assets/js/activatevalidate.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*/ 

    if (is_numeric($this->uri->segment(3)) OR $this->uri->segment(4) == '') 
    { 
     $bodyContent = "error_page"; 
    } 
    else 
    { 
     $bodyContent = "activate_form";//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->load->view('usermanagement/index', $this->data); 
} 

function activate_submit() 
{   
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric'); 

    $user_id   = $this->uri->segment(3); 
    $registration_key = $this->uri->segment(4); 

    if (($registration_key == '') OR ($user_id == '')) 
    { 
     echo json_encode(array('error' => 'yes', 'message' => 'URL was not complete!')); 
    } 
    else 
    { 
     if (!$this->form_validation->run()) 
     { 
      echo json_encode(array('error' => 'yes', 'message' => 'There was a problem submitting the form! Please refresh the window and try again!'));  
     } 
     else 
     {       
      if ($this->kow_auth->activate_user($user_id, $registration_key, $this->input->post('password'))) 
      { 
       echo json_encode(array('sucess' => 'yes', 'message' => 'Your account has been successfully activated!')); 
      } 
      else 
      {               
       echo json_encode(array('error' => 'yes', 'message' => 'The activation code you entered is incorrect or expired!')); 
      } 
     } 
    } 


} 

} 

/* End of file activate.php */ 
/* Location: ./application/controllers/activate.php */ 

差錯控制

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Error extends CI_Controller 
{ 

public function __construct() 
{ 
    parent::__construct();   
} 

public function index() 
{ 
    //Config Defaults Start 
    $msgBoxMsgs = array();//msgType = dl, info, warn, note, msg 
    $cssPageAddons = '';//If you have extra CSS for this view append it here 
    $jsPageAddons = '';//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*/ 

    $bodyContent = "error_page";//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->load->view('usermanagement/index', $this->data); 
} 

} 

/* End of file error.php */ 
/* Location: ./application/controllers/error.php */ 

任何更多的想法?

編輯 即使在下面的兩個答案我仍然收到相同的錯誤消息。

http://www.kansasoutlawwrestling.com/kowmanager/activate/

回答

1

我打算同意My_Mark的看法,他的回答是根據您最初的要求提供的。所以我們要以他的最初答案爲基礎。你的問題在於你的if-else。

您需要

public function index($param1, $param2) 

不過,我要改變它看起來更像

public function index($param1 = NULL, $param2 = NULL) 

另外請注意,這是你的控制器的索引功能,如果您有您的控制器設置在這裏你基於該功能有多個視圖運行,例如您的激活功能。然後,您需要將「index(」)替換爲「activate(」。 )其次,如果某人登錄到控制器上,您希望它達到404或發生錯誤,這就是爲什麼我改變了上面的方式,這樣你的函數就不會查找沒有設置的參數,並且可能沒有設置錯誤,所以我們默認它們爲NULL以防萬一。我的解決方案是做類似的事情..

$x = 0; 
if(($param1 !== NULL)&&($param2 !== NULL)) 
{ 

    //params not null yay.. 
    if((isset($param1))&&((trim($param1) !== '')||(!empty($param1)))) 
    { 
     if(!is_numeric($param1) 
     { 
      $x++; 
     } 
    } 
    if((isset($param2))&&((trim($param2) !== '')||(!empty($param2)))) 
    { 
     if(/*whatever your constraint for $param2 (!preg_match()) or something*/) 
     { 
      $x++; 
     } 
    } 


    if($x !== 0) 
    { 
     $bodyContent = "error_page"; 
    } 
    else 
    { 
     $bodyContent = "activate_form"; 
    } 

} 
5

更改您的路線:

$route['activate/:num/:any'] = "activate/index/$1/$2"; 

這將允許你在兩個參數傳遞到你這樣的激活控制器的索引功能:

public function index($param1, $param2) 

你將能夠做到:

if (is_numeric($param1) OR $param2 == '') 
{ 
    $bodyContent = "error_page"; 
} 
else 
{ 
    $bodyContent = "activate_form"; 
} 

希望能幫到你。

+0

+1,這就是我要說... – Alex 2012-02-06 22:07:49

+0

只有一個問題,雖然當我去這個網址它給了我一個問題。 http://www.kansasoutlawwrestling.com/kowmanager/activate/它應該只顯示404頁面。如果我做這樣的測試http://www.kansasoutlawwrestling.com/kowmanager/activate/jdahkjf/271cce33ab11ced5fd10aeca41323a3c,那麼它會出現一些奇怪的消息。 – 2012-02-06 22:09:33

+0

我添加了錯誤控制器,它是默認的404覆蓋控制器,但其相同的模板頁面代碼。不知道爲什麼它給了我這個錯誤。 – 2012-02-06 22:13:14