bla*_*elt 3 codeigniter codeigniter-2 codeigniter-routing
请参阅以下链接结构
http://stackoverflow.com/questions/10672712/voting-system-like-stackoverflow
Run Code Online (Sandbox Code Playgroud)
在上面的链接中,10672712我猜是问题ID,因为如果您检查以下链接,您将到达上面相同的位置:
http://stackoverflow.com/questions/10672712
Run Code Online (Sandbox Code Playgroud)
现在,如果您使用上述链接,那么您会注意到在浏览器的地址栏中链接会自动在问题ID后面添加问题标题(作为slug),它就像上面的第一个链接一样.
我正在尝试使用Codeigniter创建基于文章的网站.为了显示特定的文章,我有一个控制器,如下所示:
function articles(){
$id=$this->uri->segment(3);
$this->load->model('mod_articles');
$data['records']=$this->mod_articles->list_articles($id);
$this->load->view('view_article',$data);
}
Run Code Online (Sandbox Code Playgroud)
我的型号:
function list_articles($id)
$this->db->select('*');
$this->db->from('cx_article');
$this->db->join('cx_author', 'cx_author.author_id = cx_article.author_id');
$this->db->where('article_id', $id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{ return $query->row_array();
}
else {return NULL;}
}
Run Code Online (Sandbox Code Playgroud)
如果你点击这个 - > localhost/my_base_url/my_controller/my_funtion/article_id然后我的文章出现了.现在我想要实现的是如果有人点击localhost/my_base_url/my_controller/my_funtion/article_id我想在article_id之后自动添加文章标题为slug.(就像我上面给出的例子)
你能告诉我怎么做吗?
谢谢:)
PS在我的数据库表中,我有一个名为的列article_slug,其中我将我的文章标题存储为slug(例如:voting-system-like-stackoverflow).
控制器/ my_controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_controller extends CI_Controller {
function my_function($int_id = NULL, $str_slug = '') {
$row = $this->db->get_where('cx_article', array('article_id' => $int_id))->row();
if ($row and ! $str_slug) {
$this->load->helper('url');
$str_slug = url_title($row->title, 'dash', TRUE);
redirect("my_controller/my_function/{$int_id}/{$str_slug}");
}
// Run the rest of the code here
}
}
Run Code Online (Sandbox Code Playgroud)
没有必要在数据库中有一个slug列,因为你没有根据你的slug识别任何东西,也不是唯一的.
| 归档时间: |
|
| 查看次数: |
5599 次 |
| 最近记录: |