向CodeIgniter中的index()传递参数


Passing parameters to index() in CodeIgniter

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

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()函数,但是如果我试图传递参数给它,索引函数会抛出错误。

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

1)你需要 index http://www.example.com/search/index/search-keyword
2)或者您需要使用路由 $route['search/(:any)'] = 'search/index/$1';
3)或者尝试remap: 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函数的URL映射应该是最后一个

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

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
    }
}