Passing multiple variables in URL using codeignite

2019-02-12 12:46发布

问题:

sorry to bother but I was hoping someone could help me out with a quite mundane issue that I am having within CI. I am able to send a variable via the URL using CI's examples eg:

http://localhost/project/main/getproduct/24

within the getproduct() method of my main controller I can get the variable sent 24, without an issue.

however I now want to pass two variables via the URL, but I have no idea how to do this or whether CodeIgniter will allow me to do this. could someone please show me how to pass 2 variables in CI and a method that can retrieve them I have tried:

http://localhost/project/main/getproduct/24/45

and then within my getproduct method:

public function getproduct($productID, $factoryID){
  .....
}

but I'm finding my method can get the first variable without an issue but not the second variable. Could anyone point me in the right direction please. Many thanks in advance.

回答1:

You can use uri to retrieve values in your url

Here is an example

public function getproduct()
{
  $productID =  $this->uri->segment(3);
  $factoryID =  $this->uri->segment(4);
  // ProductID will be 25
  // Factory ID will be 45
}

Then you can just use the values as you please



回答2:

The accepted answer will work for this particular issue, but will not work if the url ever changes. To access multiple variables in your controller, simply add to the function definition.

http://localhost/project/main/getproduct/24/45

class Main extends CI_Controller {

    public function getproduct($productID = 0, $factoryID = 0)
    {
      // ProductID will be 25
      // Factory ID will be 45
    }
}

Reference: CodeIgniter User Guide



回答3:

You must set a route in config/routes.php to parse the items.

It looks like:

   $route["getproduct/(:any)/(:num)"]="main/changequestion/$1/$2"

Then i hope it will work.



回答4:

If someone else runs into this using CI3. In CodeIgniter 3 no special route is needed. Not sure if it also works on CI2 now.

You can access those URI segments using parameters just like that:

http://localhost/project/main/getproduct/24/45

public function getproduct($productID, $factoryID){
  .....
}


回答5:

http://example.com/project/main/getproduct/24/45

To get '45', you can do this:

 $id1 =  $this->uri->segment(3);
 echo $id1; //output is 45


回答6:

Passing URI Segments to your methods

If your URI contains more than two segments they will be passed to your method as parameters.

For example, let’s say you have a URI like this:

example.com/index.php/products/shoes/sandals/123

Your method will be passed URI segments 3 and 4 (“sandals” and “123”):

<?php
class Products extends CI_Controller {

        public function shoes($sandals, $id)
        {
                echo $sandals;
                echo $id;
        }
}

Important!!! If you are using the URI Routing feature, the segments passed to your method will be the re-routed ones.

Refer to this link as Codeigniter Official Guide. Codeigniter Official Guide.



回答7:

Solution of this problem is using of _remap() function. You just need to add this function before index() function

function _remap($method, $args)
{

       if (method_exists($this, $method))
       {
           $this->$method($args);
       }
       else
       {
            $this->Index($method, $args);
       }
}

I hope this will solve your problem.