CodeIgniter - unlimited options?

I am currently using CodeIgniter.

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

So in the controller it will be something like

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

and I could call him

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

How can I customize this?

+3
source share
3 answers

You can get the associated array of URI segments using a function uri_to_associn

+6
source

You can 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

will create an array of parameters.

+6
source

, () , , .

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

/chicken/red+tall+fat

Tags are easily implemented in CodeIgniter with one parameter.

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

/hen-house.bunk4.topshelf/chicken

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

0
source

Source: https://habr.com/ru/post/1776061/


All Articles