如何在CodeIgniter中自定义路由?

时间:2015-02-24 10:57:19

标签: php codeigniter

我正在使用CodeIgniter。例如,我的网站是test.com

我的默认控制器为Home

Home控制器代码

class Home extends CI_Controller
{
    public function index($firstname = NULL, $lastname = NULL)
    {
        if(!$firstname && !$lastname)
        {
            $this->load->view('home_view');
        }
        else
        {
            echo "First Name = " . $firstname . "<br>";
            echo "Last Name = " . $lastname ;
        }   
    }

}

当输入test.com之类的网址时,会生成输出格式home_view,并且没问题。

当输入test.com/home/index/Michael/Clarke之类的网址时,我会收到此输出:

First Name = Michael
Last Name = Clarke

我想在test.com/Michael/Clarke而非test.com/home/index/Michael/Clarke之类的网址上面。

如何删除home/index

1 个答案:

答案 0 :(得分:2)

application\config\route.php

//UPDATE
// to achive example.com/firstname/lastname, use this route.
$route['(:any)/(:any)']                  = "test/index/$1/$2";

在你的控制者中

 public function index($first, $second){

        echo "first ".$first."<br>";
        echo "second ".$second;

    }//end function

将打印example.com/ARIFUL/haque

first ARIFUL 
second haque