Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

php - Codeigniter - SEO Friendly URL Structure (Slug Implementation)


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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How to use slug?

Will explain with an example:
URL - http://www.example.com/products/apple-iphone-5S-16GB-brand-new/

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
...
...


I've also answered on slug before. Check if it helps.
How to remove params from url codeigniter


Edit:

For this you have to do below changes -

1) Create below 2 tables

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 (create new file)

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 (create new file)

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 (create new file)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...