如何在 CodeIgniter 中自定义路由?
How to customise the route in 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
这样的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//";
在你的控制器中
public function index($first, $second){
echo "first ".$first."<br>";
echo "second ".$second;
}//end function
将为 example.com/ARIFUL/haque
打印
作为
first ARIFUL
second haque
我正在使用 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
这样的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//";
在你的控制器中
public function index($first, $second){
echo "first ".$first."<br>";
echo "second ".$second;
}//end function
将为 example.com/ARIFUL/haque
打印
作为
first ARIFUL
second haque