【问题标题】:codeigniter alternative to Get variables获取变量的 codeigniter 替代方案
【发布时间】:2010-11-26 00:52:26
【问题描述】:

我一直在寻找一种在 codeigniter 中传递“GET”变量的方法,结果遇到了这个问题: link text

我想知道如何实现它。

例如:

www.website.com/query 会给我数据库中的每个条目。

通常我会这样做

www.website.com/query/?id=5 以获取等效条目。

当我尝试以 CI 方式做到这一点时:

www.website.com/query/id/5

我收到一个 404 错误,因为它正在寻找一个名为 id 的类但找不到它。

有什么办法可以一步一步来做到这一点吗?

谢谢。

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    使用 Codeigniter 开发人员打算使用的方法实现此目的的两种好方法。

    选项一:

    如果您总是希望出现“id”参数,您可以利用在要调用的方法(函数)之后立即在 URI 中传递值的特性。

    传递/[controller]/[method]/[value]的示例:

    http://www.website.com/query/index/5
    

    然后您将访问“id”的值作为函数的预期参数。

    Class Query extends Controller {
    ...
    
        // From your URL I assume you have an index method in the Query controller.
        function index($id = NULL)
        {
            // Show current ID value.
            echo "ID is $id";
            ...
        }
        ...
    }
    

    选项二:

    如果您希望除了 ID 之外还允许传递许多参数,您可以将所有参数作为键=>值对以任意顺序添加到 URI 段。

    传递/[controller]/[method]/[key1]/[val1]/[key2]/[val2]/[key3]/[val3]的示例:

    http://www.website.com/query/index/id/5/sort/date/highlight/term
    

    然后,您将使用 URI 类中的 uri_to_assoc($segment) 函数将来自第 3 段(“id”)的所有 URI 段解析为键=>值对数组。

    Class Query extends Controller {
    ...
    
        // From your code I assume you are calling an index method in the Query controller.
        function index()
        {
            // Get parameters from URI.
            // URI Class is initialized by the system automatically.
            $data->params = $this->uri->uri_to_assoc(3);
            ...
        }
        ...
    }
    

    这将使您可以轻松访问所有参数,并且它们可以在 URI 中按任何顺序排列,就像传统的查询字符串一样。

    $data->params 现在将包含一个 URI 段数组:

    Array
    (
        [id] => 5
        [sort] => date
        [highlight] => term
    )
    

    一个和两个的混合体:

    您还可以混合使用这些方法,其中 ID 作为预期参数传递,其他选项作为键 => 值对传递。当需要 ID 并且其他参数都是可选的时,这是一个不错的选择。

    传递/[controller]/[method]/[id]/[key1]/[val1]/[key2]/[val2]的示例:

    http://www.website.com/query/index/5/sort/date/highlight/term
    

    然后,您将使用 URI 类中的 uri_to_assoc($segment) 函数将第 4 段(“排序”)中的所有 URI 段解析为键=>值对数组。

    Class Query extends Controller {
    ...
    
        // From your code I assume you are calling an index method in the Query controller.
        function index($id = NULL)
        {
            // Show current ID value.
            echo "ID is $id";
    
            // Get parameters from URI.
            // URI Class is initialized by the system automatically.
            $data->params = $this->uri->uri_to_assoc(4);
            ...
        }
        ...
    }
    

    $id 将包含您的 ID 值,$data->params 将包含您的 URI 段数组:

    【讨论】:

      【解决方案2】:

      您仍然可以使用 GET 参数,它们只是映射到控制器成员函数参数:

      test.com/query/id/4
      

      将映射到控制器:

      $query->id($id);
      

      这假设您已在 CI 应用程序的 controllers 文件夹中正确添加了查询控制器和成员函数。

      您还可以使用表单和 CI 输入类将参数值作为 POST 参数传递。

      【讨论】:

        【解决方案3】:

        使用 $this->uri->uri_to_assoc(2) 2 是一个偏移量,因为您在第二个段中开始您的段关联数组。您还需要一个路由来使 /query 映射到控制器和方法(除非您在 index() 方法中执行此操作)。

        所以,这个网址:

        /query/id/foo/key/bar

        可以使用:

        $get = $this->uri->uri_to_assoc(2);
        
        echo $get['id']; // 'foo'
        echo $get['key']; // 'bar'
        

        这不是很好,但它有效。

        【讨论】:

        • 你提到它不是很好,有什么更好的选择?
        • 如果有更好的选择,我会建议的。如果 CodeIgniter 不首先处理 $_GET 变量会更好,但这是我们希望为 2.0 或社区分支排序的东西。
        • 这实际上是一种使用 URI 段作为变量的好方法,也是 CI 开发人员的预期方法!
        • URL 有一个错误...它应该指向一个控制器和一个方法... /query/index/id/foo/key/bar
        • 另一种选择,正如我上面所概述的,使用预期(必需)值。 ".../query/index/foo/bar/var3" 并且您的函数会将每个参数定义为预期参数:"function index($id = NULL, $key = NULL, $var3 = NULL)"
        猜你喜欢
        • 2017-05-13
        • 2018-01-15
        • 2013-10-30
        • 1970-01-01
        • 1970-01-01
        • 2015-11-26
        • 1970-01-01
        相关资源
        最近更新 更多