Passing multiple variables in URL using codeignite

2019-02-12 12:41发布

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.

7条回答
爷、活的狠高调
2楼-- · 2019-02-12 13:15

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.

查看更多
Evening l夕情丶
3楼-- · 2019-02-12 13:20

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){
  .....
}
查看更多
聊天终结者
4楼-- · 2019-02-12 13:25

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

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-02-12 13:26

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.

查看更多
Rolldiameter
6楼-- · 2019-02-12 13:27

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.

查看更多
Anthone
7楼-- · 2019-02-12 13:33

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
查看更多
登录 后发表回答