我对CodeIgniter和 我想将我的网址更改为
到
在带有路由的CI中。如何使用URL Route实现此目的?
我的餐桌发帖
posts_id
posts_name
我的主视图(以拨打电话)
我使用网址标题作为网址:
<a href="<?php echo base_url("{$v['posts_id']}/".url_title($v['posts_name'], "-", true)) ?>">
我的帖子控制器
<?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);
}
}
我的帖子模型
<?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();
}
}
我的路线
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['(:num)'] = 'posts/details/$1';
$route['(:num)/(:any)'] = 'posts/details/$1/$2';
答案 0 :(得分:1)
您已经接近答案
$route['(:any)-(:num)'] = 'posts/details/$2/$1';
然后查看
<a href="<?php echo base_url(url_title($v['posts_name'], "-", true)."-{$v['posts_id']}".) ?>">
仅此而已。但我建议您将网址写得更清楚,因为您可以拥有多个控制器!如果您有 comments 控制器并想从中获取数据,该怎么办? (:any)表示可以是任何