Codeigniter - SEO 友好的 URL 结构(Slug 实现)

2021-12-19 00:00:00 slug php magento wordpress codeigniter


我想在 codeigniter 框架中开发一个网站,我可以在其中通过 slug 访问任何网页.

例如,就像 WordPress 和 Magento 一样,我们可以通过 www.sitename.com/category_type/category_detailpage

访问类别页面,而且我们也可以通过在主 URI www.sitename.com/之后添加其 slug 直接访问该 Category_detailcategory_detailpage.

所以我的问题是,如果您在 Codeigniter 中有任何针对此 Slug 目录的案例研究项目代码,我必须如何设计数据库中的 slug 表架构,请尽快告诉我.

提前致谢!


I want's to develop a website in codeigniter framework in which i can access any webpage via slug.

For example just like WordPress and Magento we can access category page by www.sitename.com/category_type/category_detailpage

and also we can access that Category_detail directly just by adding its slug after main URI www.sitename.com/category_detailpage.

So my question is that how i have to design schema of slug table in database if you have any case study Project Code for this Slug Directory in Codeigniter than please let me know as soon as possible.

Thanks in Advance!

推荐答案

如何使用 slug?

How to use slug?

举例说明:
网址 - http://www.example.com/products/apple-iphone-5S-16GB-brand-new/

1) 假设您有一个产品页面,当然产品页面需要来自 URL 的一些数据来了解要显示的产品.
2) 在我们使用从 URL 获取的 id 查询我们的数据库之前.但现在我们将做同样的事情(查询我们的数据库),只是用 slug 替换 id,就是这样!
3) 因此在您的数据库中添加一个名为 slug 的附加列.下面将是您更新后的产品数据库结构(仅作为示例).

1) Assuming you are having a product page and ofcourse product page needs some data from URL to understand which product to display.
2) Before we were querying our database using the id we are getting from the URL. But now we'll do the same thing (querying our database) just replacing id with slug and thats it!
3) Hence adding an additional column in your database named slug. Below will be your updated product database structure (just an example).

Columns                       Values

id (int(11), PK)              1
title (varchar(1000))         Apple iPhone 5S 16GB
slug (varchar(1000))          apple-iphone-5S-16GB-brand-new
price (varchar(15))           48000
thumbnail (varchar(255))      apple-iphone-5S-16GB-brand-new.jpg
description (text)            blah blah
...
...


我之前也回答过 slug.检查它是否有帮助.
如何从 url codeigniter 中删除参数


为此,您必须进行以下更改 -

For this you have to do below changes -

1) 创建以下 2 个表

slug_table:

id (PK)   |    slug    |   category_id (FK)


category_table:

id (PK)   |  title  |  thumbnail  |  description


2) config/routes.php

$route['/(:any)'] = "category/index/$1";


3) models/category_model.php(创建新文件)

class Category_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->db = $this->load->database('default',true);
    }

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

        if($query->num_rows() > 0)
            return $query->row();
        return false;
    }

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

        if($query->num_rows() > 0)
            return $query->row();
        return false;
    }
}


4) controllers/category.php(创建新文件)

class Category extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('category_model');
    }

    public function index($slug)
    {
        $sl = $this->category_model->get_slug($slug);

        if($sl)
        {
             $data['category'] = $this->category_model->get_category($sl->category_id);
             $this->load->view('category_detail', $data);
        }
        else
        {
             // 404 Page Not Found
        }
    }
}


5) views/category_detail.php(创建新文件)

<label>Category title: <?php echo $category->title; ?></label><br>
</label>Category description: <?php echo $category->description; ?></label>

相关文章