Codeigniter博客应用程序:仅在单一帖子网址中用slug替换帖子ID

时间:2018-10-14 09:42:49

标签: php codeigniter

我正在使用Codeigniter 3.1.8和Bootstrap 4中的基本博客应用

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

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

enter image description here

这些标签是由帖子标题制成的,这些代码分别在 Posts_model 模型和 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;
}

为了实现我的SEO目标,我已经考虑并尝试了 this tutorial 中的方法,但是由于有许多功能需要ID(例如,通过AJAX删除帖子),而其他功能不要要求 sl(编辑帖子)我无法(也不愿意)在控制器中实质性地修改帖子查看方法,

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;
    }
}

单篇帖子视图中的一种简单的,较少“干扰性”的方法来用帖子替换帖子ID?

3 个答案:

答案 0 :(得分:1)

模型更新

替换

3D Start Rotation.

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

在控制器中更新

替换

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

public function post($id) {

并替换

public function post($slug) {

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

答案 1 :(得分:1)

还要更新您的路线以获得更好的用户友好网址。

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

答案 2 :(得分:0)

好吧,我想您可以在同一函数/ url上同时包含id和子段,例如/id/slug。这样,您始终拥有id,并且seo也很高兴,因为url具有页面标题(子标题)。然后在函数上,您可以拥有public function post($id,$slug="") {