【问题标题】:Passing URI segments into Codeigniter functions将 URI 段传递给 Codeigniter 函数
【发布时间】:2012-04-25 23:45:49
【问题描述】:

假设我有一个通过 URI 接收变量的 view 方法。

例如。 http://www.domain.com/item/view/200

将传入的变量转换为正确的类型有意义吗?在这种情况下,将$item_id 变成int,这是预期的结果。

例如在控制器中...

function view($item_id) {
    if ( $this->item_model->checkItem( (int) $item_id )) {
    ...
    }
}

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    是的,像你一样将变量转换为整数是可以的。

    而将段传递给函数的方式就像

    $segment = $this->uri->segment(2); //get the second segmention
               // ^ You can send a default Values as well if itemid is not set
               // Like: $this -> uri -> segment(2, '0')
    //now pass it to the function you need
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2020-06-24
    • 1970-01-01
    相关资源
    最近更新 更多