php 使用 codeigniter 在 URL 中传递多个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22766010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:15:17  来源:igfitidea点击:

Passing multiple variables in URL using codeigniter

phpcodeigniterurlgeturi

提问by TotalNewbie

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:

很抱歉打扰,但我希望有人可以帮助我解决我在 CI 中遇到的一个非常平凡的问题。我可以使用 CI 的示例通过 URL 发送变量,例如:

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

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

在我的主控制器的 getproduct() 方法中,我可以将变量发送 24,没有问题。

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:

但是我现在想通过 URL 传递两个变量,但我不知道如何执行此操作或 CodeIgniter 是否允许我执行此操作。有人可以告诉我如何在 CI 中传递 2 个变量以及一种可以检索它们的方法我试过:

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

and then within my getproduct method:

然后在我的 getproduct 方法中:

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.

但我发现我的方法可以毫无问题地获得第一个变量,但不能获得第二个变量。谁能指出我正确的方向。提前谢谢了。

回答by Donovan

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.

接受的答案将适用于此特定问题,但如果 url 发生更改,则将不起作用。要访问控制器中的多个变量,只需添加到函数定义中即可。

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

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

参考:CodeIgniter 用户指南

回答by Chitowns24

You can use urito retrieve values in your url

您可以使用uri检索您的网址中的值

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

然后你可以随意使用这些值

回答by Alex7

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

您必须在 config/routes.php 中设置一个路由来解析这些项​​目。

It looks like:

看起来像:

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

Then i hope it will work.

然后我希望它会起作用。

回答by Firze

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.

如果其他人使用 CI3 遇到此问题。在 CodeIgniter 3 中不需要特殊的路由。不确定它现在是否也适用于 CI2。

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

您可以使用如下参数访问这些 URI 段:

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

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

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

回答by Sani Kamal

You can use urito retrieve values in your url http://localhost/project/main/get_product/12/23

您可以使用uri在您的 url http://localhost/project/main/get_product/12/23 中检索值

Here is an example

这是一个例子

public function get_product(){
  $product_id =  $this->uri->segment(3);  // Product id will be 12
  $factory_id =  $this->uri->segment(4);  // Factory id will be 23

}

Then you can just use the values as you please

然后你可以随意使用这些值

回答by Faisal

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

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

To get '45', you can do this:

要获得“45”,您可以这样做:

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

回答by Mohammad Naim Dahee

Passing URI Segments to your methods

将 URI 段传递给您的方法

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

如果您的 URI 包含两个以上的段,它们将作为参数传递给您的方法。

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

例如,假设您有一个这样的 URI:

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

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

您的方法将传递 URI 段 3 和 4(“sandals”和“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.

重要的!!!如果您正在使用 URI 路由功能,则传递给您的方法的段将是重新路由的段。

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

请参阅此链接作为 Codeigniter 官方指南。 Codeigniter 官方指南。

回答by Usama Khalid

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

这个问题的解决方案是使用_remap()函数。你只需要在 index() 函数之前添加这个函数

function _remap($method, $args)
{

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

I hope this will solve your problem.

我希望这能解决你的问题。