激活路由和控制器

时间:2012-05-22 18:29:04

标签: php codeigniter

我想要做的是弄清楚如果我需要这种情况的路线。在用户注册我的网站后,他们会收到一封验证邮件,在该邮件中他们点击一个链接并发送到激活控制器,在那里它验证第一个参数是数字,第二个是字符串,然后如果它们与一个记录匹配在一起db然后用户被成功激活。

这是我对我的控制器所拥有的:

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

class Activate extends CI_Controller { 

public function __construct()
{
    parent::__construct();
    $this->load->library('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 =  '';//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*/

    $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(!preg_match('/^[A-Za-z0-9]+$/', $param2))
            {
              $x++;
            }
        }


        if($x !== 0)
        {
           $bodyContent = $this->config->item('defaultTemplate') ."error_page";
        }
        else
        {
           $bodyContent = $this->config->item('defaultTemplate') ."/usermanagement/forms/activate";//which view file
        }

    }
    else
    {
        $bodyContent = "error_page";
    }

    /***********************************************************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['siteTitle'] = $siteTitle;//defaults can be changed via models/metatags.php
    $this->data['bodyType'] = $bodyType;
    $this->data['bodyContent'] = $bodyContent;
    $this->load->view($this->config->item('defaultTemplate') .'/usermanagement/index', $this->data);
}

}

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

我的网址看起来像siteurl.com/activate/10000/7dfdao87fda8f7

1 个答案:

答案 0 :(得分:0)

如果您的网址为http://example.com/activate/account/12345/abcde,那么您将不需要路线。

如果它是http://example.com/activate/12345/abcde那么你需要一条这样的路线:

$route["activate/(:num)/(:any)"] = "activate/account/$1/$2";

理所当然地认为activate控制器有一个名为account的方法,用于激活帐户。

这样做是取第一个括号内的值(这是一个数字)并将其插入$1的位置,然后取第二个括号内的值(即任何字符)并将其插入到... $2

然后您需要使用以下控制器/方法:

class Activate extends CI_Controller {
    public function account ($var1, $var2) {
        // ...process vars etc
    }
}

如果使用索引方法,则需要将变量传递给它,然后在路径中调用它:

class Activate extends CI_Controller {
    public function index ($var1, $var2) {
        // ...process vars etc
    }
}

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

这是因为通过将其更改为activate/$1/$2,您告诉它在$1中查找方法,而不是索引方法。