控制器在URL中的多个变量

Jef*_*son 0 php codeigniter

我正在试图弄清楚如何处理我的路线和控制器.

用户在我的网站上注册后,他们会收到一封电子邮件,其中包含指向激活控制器的链接,该控制器会询问其密码以确认该帐户.但是我正在尝试向我的控制器添加一个if语句,以便它验证在url中激活结束时有两个参数,如果没有则会显示我的错误页面.还需要验证第一个参数是否为数字.

出于某种原因,我有正确的设置,不知道是什么.

这是我用于路线的内容

$route['activate/:num/:any'] = "activate";
Run Code Online (Sandbox Code Playgroud)

控制器:

<?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 */ 
Run Code Online (Sandbox Code Playgroud)

错误控制器

<?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 */ 
Run Code Online (Sandbox Code Playgroud)

还有其他想法吗?

编辑 即使有下面的两个答案,我仍然得到相同的错误消息.

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

小智 5

将您的路线更改为:

$route['activate/:num/:any'] = "activate/index/$1/$2";
Run Code Online (Sandbox Code Playgroud)

这将允许您将两个参数传递到激活控制器的索引函数,如下所示:

public function index($param1, $param2)
Run Code Online (Sandbox Code Playgroud)

然后你就可以做到:

if (is_numeric($param1) OR $param2 == '')
{
    $bodyContent = "error_page";
}
else
{
    $bodyContent = "activate_form";
}
Run Code Online (Sandbox Code Playgroud)

希望对你有所帮助.