【问题标题】:Codeigniter - SEO Friendly URL Structure (Slug Implementation)Codeigniter - SEO 友好的 URL 结构(Slug 实现)
【发布时间】:2014-11-06 04:58:56
【问题描述】:


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

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

访问类别页面,也可以通过在主 URI www.sitename.com/ 之后添加其 slug 直接访问该 Category_detail category_detailpage。

所以我的问题是,如果您在 Codeigniter 中有此 Slug 目录的任何案例研究项目代码,那么我必须如何在数据库中设计 slug 表的架构,而不是请尽快让我知道。

提前致谢!

【问题讨论】:

    标签: php wordpress codeigniter magento slug


    【解决方案1】:

    如何使用slug?

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

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

    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。检查它是否有帮助。
    How to remove params from url codeigniter


    编辑:

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

    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>
    

    【讨论】:

    • 我不想这样做,我想像 wordpress 和 magento 框架管理一样管理 1 个表中的所有网页 slug,然后仅通过 slug 访问任何网页我不想要任何控制器和方法名称显示在 URL 中。那么我将如何管理它?
    • 我能不能用 /products/ 作为类别标题 ..(例如 www.examples.com/foods/sweet-apple.html )..请给我看..
    猜你喜欢
    • 1970-01-01
    • 2016-05-28
    • 2011-07-15
    • 2011-12-24
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 2011-08-25
    相关资源
    最近更新 更多