[英]Passing URI segments into Codeigniter functions
Let's say I have a view
method that receives a variable via the URI. 假设我有一个
view
方法通过URI接收变量。
ex. 例如 http://www.domain.com/item/view/200
http://www.domain.com/item/view/200
Does it make sense to turn the incoming variable into the correct type? 将传入变量转换为正确类型是否有意义? In this case, making
$item_id
into int
which is what is expected. 在这种情况下,将
$item_id
为int
。
For example, in the controller ... 例如,在控制器中...
function view($item_id) {
if ( $this->item_model->checkItem( (int) $item_id )) {
...
}
}
And the way to pass the segment into function is like 将段传递给函数的方式就像
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.