将参数传递给CodeIgniter中的index()

时间:2014-08-05 07:31:30

标签: php codeigniter url

我的应用程序有以下结构:

myapp/index.php: index page
myapp/mycont/myfunc/productID: dedicated page for product

index.php包含可以打开myapp/mycont/myfunc/productID

的链接

因此,控制器功能定义为:

index() {...}
myfunc($id) {...}

因为,我的两个函数几乎相同,我想删除myfunc()函数并将URL更改为:

myapp/: index page
myapp/productID: dedicated page for product

上述两个URL都应该使用index()函数,但如果我尝试将参数传递给它,则索引函数会抛出错误。

2 个答案:

答案 0 :(得分:3)

将参数传递给CI中的索引函数:

1)您需要 index http://www.example.com/search/index/search-keyword
2)或者您需要使用路线 $route['search/(:any)'] = 'search/index/$1';
3)或尝试重新映射https://ellislab.com/codeigniter/user-guide/general/controllers.html#remapping


编辑(示例):

$route['abc/campaigns/add_webengage_feedback_leads'] = "abc/campaigns/add_webengage_feedback_leads";
$route['abc/campaigns/add_webengage_survey_leads'] = "abc/campaigns/add_webengage_survey_leads";
$route['abc/campaigns/add_canvass_data'] = "abc/campaigns/add_canvass_data";
$route['abc/campaigns/add_connecto_data'] = "abc/campaigns/add_connecto_data";
$route['abc/campaigns/(:any)'] = "abc/campaigns/index/$1";

注意:映射到index功能的网址应该是最后一个。

答案 1 :(得分:2)

假设您已从index.php中删除了URL,则可以按此流程进行操作,

function index(){
    if( $this->uri->segment(3) ){     //if an product id is set
        $id = $this->uri->segment(3); //this is the product id in third segment of URL
    }else{                            //display the normal index page

    }
}