【问题标题】:Codeigniter get parameters from urlCodeigniter 从 url 获取参数
【发布时间】:2016-02-14 10:34:00
【问题描述】:

我有一个网址“http://localhost/codeigniter/index.php/manual_export/”。我需要从假设为 id 的 url 中获取最后一段。例如“http://localhost/codeigniter/index.php/manual_export/2”。我需要得到最后一段是“2”。

我厌倦了使用以下代码:

$id = end($this->uri->segment_array()); 

当我不向 url 添加“2”并给我“manual_export”时,这有效。但是,当我将 id 传递给 url 时,出现错误“找不到您请求的页面。”。我认为这与路由有关。我该如何解决这个错误。

【问题讨论】:

    标签: codeigniter url post


    【解决方案1】:

    另一种方法是定义路由,然后将其转换为参数

    例如,如果您的控制器被称为 manual_export 并且方法是 getrecord

    在文件 application/routes.php 中

    $route['manual_export/(:any)'] = "manual_export/getrecord/$1";
    

    在你的控制器手册_出口中

    function getrecord($id){ // etc etc }
    

    【讨论】:

      【解决方案2】:

      你应该使用:

      $this->uri->segment(n);//in your case n == 2 count starts just after index.php
      

      Docs.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-24
        • 2021-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多