[英]CodeIgniter URL Route with ID and title
Im new to CodeIgniter and I would like to change my URL from 我是CodeIgniter的新手,我想将网址从
http://test.com/1/test-post http://test.com/1/test-post
to 至
http://test.com/test-post-1 http://test.com/test-post-1
in CI with route. 在CI与路线。 How can i achieve this with URL Route ?
如何使用URL Route实现此目的?
My Table Posts 我的桌子上的帖子
posts_id
posts_name
My Home View (to call the posts) 我的主视图 (给帖子打电话)
Im using url title for the url: 我正在使用URL标题作为URL:
<a href="<?php echo base_url("{$v['posts_id']}/".url_title($v['posts_name'], "-", true)) ?>">
My Posts Controller 我的帖子控制器
<?php
defined('BASEPATH') OR exit ('No direct script access allowed');
class Posts extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Posts_model');
}
public function index()
{
show_404();
}
public function details($posts_id=NULL,$posts_name='') {
$data['title'] = $this->Posts_model->select_content_by_id($posts_id);
$data['data'] = $this->Posts_model->select_content_by_id($posts_id);
$this->load->view('posts',$data);
}
}
My Posts Model 我的帖子模型
<?php
defined('BASEPATH') OR exit ('No direct script access allowed');
class Posts_model extends CI_Model{
public function dsPosts()
{
return $this->db->get('posts')->result_array();
}
public function select_content_by_id($posts_id)
{
return $this->db->where('posts_id',$posts_id)->get('posts')->result_array();
}
}
My Route 我的路线
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['(:num)'] = 'posts/details/$1';
$route['(:num)/(:any)'] = 'posts/details/$1/$2';
You are near to the answer 您就在答案附近
$route['(:any)-(:num)'] = 'posts/details/$2/$1';
then in view 然后来看
<a href="<?php echo base_url(url_title($v['posts_name'], "-", true)."-{$v['posts_id']}".) ?>">
That's all. 就这样。 But I suggest you write more clearly urls because you can have more than one controller!
但我建议您将网址写得更清楚,因为您可以拥有多个控制器! What happens if you have comments controller and want to get data from it?
如果您有注释控制器,并希望从中获取数据会怎样? (:any) means it can be whatever
(:any)表示可以是任意值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.