用于将 get 参数传递给索引函数的代码点火器路由


Codeigniter routes for passing get parameter to index function

我有一个网址www.mywebsite.com/store/123456

其中 store 是我的控制器类,我在其中有一个索引函数,其中 Im 在存储后获取值,即 123456.但我无法实现它。

正如在网上找到的,我尝试将其添加到路线$route['store/(:any)'] = 'store/index';也尝试过

$route['store/(:any)'] = 'store/index/$1';

但似乎不起作用。请帮帮我。

在控制器索引功能中是

public function index()
    {
        echo $this->uri->segment(1);
    }

但它不起作用。Pleae help

正在调用123456()方法而不是index()方法,因此您得到 CI 的 404。

最简单的方法是使用这种路由

$route['store/(:any)'] = 'store/index/$1';

并在它的基础上将参数添加到索引函数中,在您的情况下

public function index($parameter)
{
    echo $this->uri->segment(2);
}

请注意,我更改了段参数,请参阅文档。


使用 _remap()

function _remap($parameter){
   $this->index($parameter);
}
function index($p) 
{
    echo $p; //shows 123456 
}
如果我

没记错的话:

段 (n) 在路由发生之前为您提供 URI 的段

rsegment(n) 为您提供路由后 URI 的段(如果路由发生,如果没有路由,则与段相同)

所以对于/store/123456 被重新路由到/store/index/123456

segment(1) == rsegment(1) == 'store'
              rsegment(2) == 'index'
segment(2) == rsegment(3) == '123456'

路由:

$route['terms_and_conditions'] = 'SO_front/page/1';

控制器:

public function page($id)
    {
        echo $id;
    }

输出:1

这是我对 CI3 的解决方案...我更喜欢Laravel进行高级路由,orm,restapi,在vuejs中构建。

$route['store/(:any)'] = 'store/index/';
public function your index(){
    $parameter=$this->uri->segment(2);
    //use parameter for sql query.
}

假设您的路线$route[store/product/update/(:any)],然后$parameter=$this->uri->segment(4)

问题?。如果计划更改路由名称(包括视图、控制器和自定义路由),则需要更改整个文件代码。