PHP-Codeigniter网址空间 [英] Php - Codeigniter url spaces
问题描述
我要尝试的是单击特定产品时,我想输入产品名称,而不是在我的网址中显示产品ID。
我今天要做的是base_url / controller / method / id
I'm trying to do is when i clicked my specific product, instead of the id of my product would appear in my url, i want to put the name of the product . what i did today is base_url/controller/method/id
我想做的是base_url / controller / method / product-name或base_url / controller / method / product_name
i want to do is base_url/controller/method/product-name or base_url/controller/method/product_name
,但是在我的代码中发生了什么事,我的产品名称中的所有空格都被%20占据了,我该怎么做才能用下划线或%代替%20破折号或以任何安全的方式在url中调用产品名称的方法。
but in my code what happened is in my product name all the spaces is occupied by %20 what should i do to replace the %20 by either underscore or dash or whatever safe way to call the product name in url.
以及我在该$ name(product_name)上获得的任何信息,我都会将其传递给我的模型。
and whatever i get on that $name(product_name) i will pass it to my model.
我的控制器
public function details($name){
$prod_row = $this->PromoModel->getpromorow($name);
$data['promodetail'] = $prod_row;
$data['promo'] = $this->PromoModel->get_promo();
$data['message']= "<h1> NO PROMOS AVAILABLE</h1>";
$this->load->view('include/header',$data);
$this->load->view('include/nav');
$this->load->view('promo/PromoDetail', $data);
$this->load->view('include/footer');
}
我的模型
public function getpromorow($name) {
$this->db->where('name', $name);
$query = $this->db->get('promo');
return $query->row();
}
public function get_promo(){
$query = $this->db->get('promo');
return $query->result();
}
我的观点
<a href="<?= base_url(). 'promos/details/' . $promo_row->name ?>"> </a>
推荐答案
这应该可行,
<a href="<?= base_url(). 'promos/details/' . str_replace(' ', '_', $promo_row->name); ?>"> </a>
就-
与 _
如果要路由到它们,例如说产品名称成为控制器中的方法,我认为PHP不允许-
在方法名称中,但也不允许使用空格。.您可以使用 __ call()
作弊,但这是另一天的故事。
As far as -
vs _
if you are routing to them, for example say that product name becomes a method in your controller, I don't think PHP allows -
in method names, but it doesn't allow spaces either.. You can cheat using __call()
but that's a other story for a different day.
如果它是一个参数,那么它应该没什么大不了的。
If its a parameter, then it shouldn't matter as much. Just if it is a method/function name.
-注意-这不是配置项特定的错误,请查看任何URL,告诉我是否在其中看到空格。
-Note- this is not a CI specific error, look at any url, tell me if you see spaces in them.
更新:作为建议,最好的方法是通过使用专用于url名称的字段(例如 slug
等),然后在制作新产品时对名称进行转换。然后,这是您可以控制的可编辑数据。例如,如果您的商品名称中带有-
,该怎么办?这可能会引起问题。您可以通过为此单独准备一条数据来避免这种情况,它甚至可能像SKU编号等。设置起来需要做更多的工作,但从长远来看更易于维护。
UPdate: as a suggestion the best way to handle this is by having a field dedicated to the url name ( such as slug
or such ) and when a new product is made just do the transform on the name. Then it's an editable piece of data you have control over. For example what if you have a product with a -
in it's name. This will probably cause issues. You can avoid that by having a separate piece of data just for that, it could even be like a SKU number etc. It's more work to setup, but in the long run more maintainable.
这篇关于PHP-Codeigniter网址空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!