問題描述
我想在 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>
這篇關于Codeigniter - SEO 友好的 URL 結構(Slug 實現)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!