CodeIgniter - unlimited parameters?

点点圈 提交于 2019-12-06 06:20:04

问题


I am currently using CodeIgniter.

I am trying to write a function that can take an unlimited number of paramaters.

So in the controller it will be something like

function test($name, $others){
    foreach($others){
       //do something
    }
}

and I could call it like

example.com/control/test/some name/param1/param2/param3/param4/param5... 

How can I set this up?


回答1:


You can get an associated array of URI segments with the uri_to_assoc function in the URI class. So in your controller, you might do something like this:

function test($name){
    $uri_seg = $this->uri->uri_to_assoc(4);

    foreach($uri_seg as $para){
        // Do something with each of the URI segments passed in here after $name
    }
}



回答2:


you could also do it like this:

function foo($params=array())
{
    $params=func_get_args();
    //print_r($params);
}

so any url like:

site.com/controller/foo/param1/param2/param3/param4

would create an array of parameters.




回答3:


It's not clear why you need an unlimited (variable) number of arguments, but you may want to consider why you need them.

You can use alternative URI semantics for certain types of arguments. Often the URI design is the path to a thing in a system. Unlimited parameters may not refer to the resource, but rather describe its attributes. Tags are a good example of this.

/chicken/red+tall+fat

Tags implement easily in CodeIgniter with a single parameter.

If the parameters refer to a node, you can consider a similar approach:

/hen-house.bunk4.topshelf/chicken

Or, if the hierarchy is important as a path, you can use the func_get_args() solution above.



来源:https://stackoverflow.com/questions/4237220/codeigniter-unlimited-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!