我正在使用 CodeIgniter。比如我的网站是test.com
我的默认 Controller 是Home
Home
Controller 代码
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
这样的 URL 时,它会生成 home_view
的输出,并且没问题。
当输入类似 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
?
最佳答案
在你的application\config\route.php
//UPDATE
// to achive example.com/firstname/lastname, use this route.
$route['(:any)/(:any)'] = "test/index/$1/$2";
在你的 Controller 中
public function index($first, $second){
echo "first ".$first."<br>";
echo "second ".$second;
}//end function
将为 example.com/ARIFUL/haque
打印
作为
first ARIFUL
second haque
关于php - 如何在 CodeIgniter 中自定义路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28693948/