Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter博客应用程序:仅在单个帖子url中将帖子id替换为slug_Php_Codeigniter - Fatal编程技术网

Php Codeigniter博客应用程序:仅在单个帖子url中将帖子id替换为slug

Php Codeigniter博客应用程序:仅在单个帖子url中将帖子id替换为slug,php,codeigniter,Php,Codeigniter,我正在Codeigniter 3.1.8和Bootstrap 4中开发一个基本的博客应用程序 我已经在这个应用程序上工作了一段时间,因此,它有很多特性。其中大多数依赖于帖子、页面等的ID 出于SEO目的,我想使用友好的URL而不是ID来显示各个帖子,因此我在posts表中添加了slug列 slug由post标题组成,这些代码块分别位于Posts\u模型和Posts控制器中 从模型中: public function slug_count($slug){ $this->db->

我正在Codeigniter 3.1.8和Bootstrap 4中开发一个基本的博客应用程序

我已经在这个应用程序上工作了一段时间,因此,它有很多特性。其中大多数依赖于帖子、页面等的ID

出于SEO目的,我想使用友好的URL而不是ID来显示各个帖子,因此我在
posts
表中添加了
slug

slug由post标题组成,这些代码块分别位于Posts\u模型和Posts控制器中

模型中

public function slug_count($slug){
    $this->db->select('count(*) as slugcount');
    $this->db->from('posts');
    $this->db->where('slug', $slug);
    $query = $this->db->get();
    return $query->row(0)->slugcount;
}

public function create_post($post_image, $slug) {
    $data = [
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'description' => $this->input->post('desc'),
        'content' => $this->input->post('body'),
        'post_image' => $post_image,
        'author_id' => $this->session->userdata('user_id'),
        'cat_id' => $this->input->post('category'),
        'created_at' => date('Y-m-d H:i:s')
    ];
    return $this->db->insert('posts', $data);
} 
// Create slug (from title)
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$slugcount = $this->Posts_model->slug_count($slug);
if ($slugcount > 0) {
    $slug = $slug."-".$slugcount;
}
控制器

public function slug_count($slug){
    $this->db->select('count(*) as slugcount');
    $this->db->from('posts');
    $this->db->where('slug', $slug);
    $query = $this->db->get();
    return $query->row(0)->slugcount;
}

public function create_post($post_image, $slug) {
    $data = [
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'description' => $this->input->post('desc'),
        'content' => $this->input->post('body'),
        'post_image' => $post_image,
        'author_id' => $this->session->userdata('user_id'),
        'cat_id' => $this->input->post('category'),
        'created_at' => date('Y-m-d H:i:s')
    ];
    return $this->db->insert('posts', $data);
} 
// Create slug (from title)
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$slugcount = $this->Posts_model->slug_count($slug);
if ($slugcount > 0) {
    $slug = $slug."-".$slugcount;
}
为了实现我的SEO目标,我在中考虑并尝试了这种方法,但由于有许多功能需要ID(例如通过AJAX删除帖子)和其他不需要SLUG(编辑帖子)的功能,我无法(也不愿意)在控制器中实质性地修改帖子查看方法

public function post($id) {
    $data = $this->Static_model->get_static_data();
    $data['pages'] = $this->Pages_model->get_pages();
    $data['categories'] = $this->Categories_model->get_categories();
    $data['posts'] = $this->Posts_model->sidebar_posts($limit=5, $offset=5);
    $data['post'] = $this->Posts_model->get_post($id);

    if ($data['categories']) {
        foreach ($data['categories'] as &$category) {
            $category->posts_count = $this->Posts_model->count_posts_in_category($category->id);
        }
    }

    if (!empty($data['post'])) {
        // Overwrite the default tagline with the post title
        $data['tagline'] = $data['post']->title;

        // Get post comments
        $post_id = $data['post']->id;
        $data['comments'] = $this->Comments_model->get_comments($post_id);

        $this->load->view('partials/header', $data);
        $this->load->view('post');
    } else {
        $data['tagline'] = "Page not found";
        $this->load->view('partials/header', $data);
        $this->load->view('404');
    }
    $this->load->view('partials/footer');
} 
以及模型中相应的代码:

public function get_post($id) {
    $query = $this->db->get_where('posts', array('id' => $id));
    if ($query->num_rows() > 0) {
        $data = $query->row();
        // run separate query for author name
        $author_query = $this->db->get_where('authors', array('id' => $data->author_id));
        if ($author_query->num_rows() == 1) {
            $author = $author_query->row();
            $data->first_name = $author->first_name;
            $data->last_name = $author->last_name;             
        } else {
            $data->first_name = 'Unknown';
            $data->last_name = '';
        }
        return $data;
    }
}

在singe post视图中,用slug替换post id的简单、不太“侵入性”的方法是什么?

好吧,我认为您可以将id和slug放在同一个函数/url上,例如
/id/slug
。这样,你总是有你的
id
,搜索引擎优化也很高兴,因为url有页面标题(slug)。然后在你的函数上,你可以有
公共函数post($id,$slug=”“){

在模型中更新

替换

public function get_post($id) {
   $query = $this->db->get_where('posts', array('id' => $id));

在控制器中更新

替换

public function post($id) {

替换

$data['post'] = $this->Posts_model->get_post($id);


同时更新您的路由以获得更好的用户友好url

//If you categorize your blog
$route['blog-category/(:any)'] = 'blogController/blogFunction/$1';
//this will accept any uri
$route['(:any)/(:any)'] = 'blogController/blogFunction'

这可能并不总是好的做法,假设slug实际上包含一个数字(产品id或序列号)和url中存在的另一个id,这对SEO是有害的。你会怎么做?什么是最佳的方法?所以你想做的是让post/123和post/some post(其中id是123)给你同样的结果,对吗?当我移动到slug时,我这样做了…如果URL参数是数字,我只需搜索Id,否则slug就会得到帖子。是的。我应该如何实现这一点?我的帖子位于
http://localhost/ciblog/posts/post/the-post-slug
。我必须在路线中添加什么才能拥有
http://localhost/ciblog/post slug
insead?这样说。
$route['(:any)/(:any)]='posts/post/$1
//If you categorize your blog
$route['blog-category/(:any)'] = 'blogController/blogFunction/$1';
//this will accept any uri
$route['(:any)/(:any)'] = 'blogController/blogFunction'