我想将参数传递给索引函数,如果参数不是,则重定向到主页。我也添加了自定义路线。我在哪里错过了代码?
$route['main/(:any)'] = 'main/index/'; // looks like has a mistake
www.example.com - >当deafult index function
时www.example.com/females - >当deafult index具有字符串参数'female'
时class Main extends CI_Controller {
public function index($gender) {
load->view('inc/header_view');
if((isset($gender)) && ($gender =='female')){
$this->load->view('female_view');
} else {
$this->load->view('female_view');
}
}
答案 0 :(得分:1)
您可以通过以下代码
来实现此目的class Main extends CI_Controller {
public function index($gender="") {
if($gender == "")
{
$this->load->view('home');
} else {
$this->load->view('female_view');
}
}
正如您所提到的,要让此网址正常工作www.example.com
& www.example.com/females
,您需要在routes.php中添加以下内容
$route['default_controller'] = "main";
$route['(:any)'] = 'main/index/$1';
如果有任何疑问,请告诉我
答案 1 :(得分:0)
您应该尝试
$route['main'] = 'main/index';
$route['main/(:any)'] = 'main/index/$1';
或者您可以使用uri细分。
http://www.codeigniter.com/user_guide/libraries/uri.html?highlight=uri%20segments#CI_URI::segment