Codeigniter - SEO友好的URL结构(Slug实现) [英] Codeigniter - SEO Friendly URL Structure (Slug Implementation)

查看:1569
本文介绍了Codeigniter - SEO友好的URL结构(Slug实现)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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


例如就像WordPress和Magento,我们可以访问类别页面由www.sitename.com/category_type/category_detailpage

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


所以我的问题是如何我必须设计slug表在数据库中的模式如果你有任何案例研究项目代码这个Slug目录在Codeigniter比请让我知道尽快。


感谢您!

解决方案

如何使用lug??



将以示例说明:

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



1 )假设您有一个产品页面,而且产品页面需要一些来自URL的数据来了解要显示的产品。

2)在我们使用我们从URL获取的ID查询数据库之前。但现在我们将做同样的事情(查询我们的数据库),只是用slug替换id,

3)因此在数据库中添加一个名为slug的额外列。下面是您更新的产品数据库结构(只是一个例子)。

 列值

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


我也曾经回答过。检查是否有帮助。

如何从网址中删除参数代码信号器




编辑



必须进行以下更改 -



1)创建以下2个表格

  slug_table:

id(PK)| lug | category_id(FK)


category_table:

id(PK)|标题|缩略图| 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) .php (创建新文件)

 类类别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)
$ it-> load-> view('category_detail',$ data);
}
else
{
// 404页面未找到
}
}
}




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

 < label>类别标题:<?php echo $ category-> title; ?>< / label>< br> 
< / label>类别说明:<?php echo $ category-> description; ?>< / label>



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!

解决方案

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>

这篇关于Codeigniter - SEO友好的URL结构(Slug实现)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆