使用用户定义的对顺序传递多个可选参数/值对 [英] Passing multiple optional parameter/value pairs with user defined pair order
问题描述
说用户要在4或5个字段上搜索,好吗?例如。他可能想以名字,电子邮件,手机甚至页码进行搜索。
我想代码的网址如下:
$ b $
value3 / variable1 / value1
(应该有相同的结果)
或以此格式
site.com/Controller/Method/variable2/value2/variable4/ value4
一些例子可以澄清我的问题:
site.com/user/search/firstname/John/mobile/123/page/2
或:
site.com/user/search/lastname/Smith/email/gmail .com
在一句话中:我不想强迫用户使用特定的订单
您可以在控制器上使用_remap函数。
function _remap($ method,$ params)
{
$ map = array
for($ i = 1; $ i {
$ map [$ params [$ i-1] = $ params [$ i];
}
if($ method [0]!='_'&& method_exists($ this,$ method))
return $ this-> $ method ($ map);
}
如果要在所有控制器中使用它,写一个自定义控制器来扩展CI_controller和这个函数,并让所有的控制器扩展。
Say user is going to search on 4 or 5 fields, ok? eg. he might want to search by firstname, email, mobile or even page number
I want that codeigniter's url be like this:
site.com/Controller/Method/variable1/value1/variable2/value2/variable3/value3
or
site.com/Controller/Method/variable2/value2/variable3/value3/variable1/value1
(that they should have the same result)
or in this format
site.com/Controller/Method/variable2/value2/variable4/value4
some examples to clarify my question:
site.com/user/search/firstname/John/mobile/123/page/2
or:
site.com/user/search/lastname/Smith/email/gmail.com
In one sentence: I don't want to force the user to use a specific order when setting value to parameters.
You can use the _remap function on the controller.
function _remap( $method, $params )
{
$map = array();
for( $i = 1; $i < count( $params ); $i = $i + 2 )
{
$map[$params[$i-1]] = $params[$i];
}
if( $method[0] != '_' && method_exists( $this, $method ))
return $this->$method( $map );
}
If you want to use it across all your controllers, you would be better to write a custom controller to extend CI_controller with this function, and have all your controllers extend that.
这篇关于使用用户定义的对顺序传递多个可选参数/值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!