2015-02-24 119 views
3

我正在使用codeigniter。例如我的網站是test.com在CodeIgniter中需要路由幫助?

我的默認控制器是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這是確定

時是進入url like test.com/home/index/Michael/Clarke

輸出上面的url:

First Name = Michael 
Last Name = Clarke 

我上面想出來的URL像test.com/Michael/Clarke而不是test.com/home/index/Michael/Clarke

如何刪除home/index

+0

,設置此'$ route ['home'] /(:any)/(:any)=「home/index」;'。這條路線將帶你到'index()'函數。 – 2015-02-24 11:04:13

回答

2

在你application\config\route.php

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

在你controllar

public function index($first, $second){ 

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

    }//end function 

會在你的`的application/config/route.php`打印爲example.com/ARIFUL/haque 作爲

first ARIFUL 
second haque 
+0

它是好的,但我想同樣的結果之一example.com/ARIFUL/haque而不是example.com/home/ARIFUL/haque – user3048004 2015-02-24 11:25:21

+1

更改'$ route ['home /(:any)/(:any)'] =「home/index/$ 1/$ 2」;'到'$ route ['(:any)/(:any)'] =「test/index/$ 1/$ 2」;'我更新了上面的代碼。 – 2015-02-24 11:30:24

+0

完美男人非常感謝..... – user3048004 2015-02-24 11:35:16