How to change query string for url segment in codeigniter?
hello everyone ,
This is my first question, here I hope everyone understands my problem, help me ..
So this is an issue that I am working on an app based on the sports community ( CODEIGNITER ), so when the logged in user clicks on the team name or redirects the sports page to a specific team page and the url looks like below
http: // localhost / project / home? cat_id = MjY = & cat_type = Mg ==
where cat_id and cat_type are in base64encode()
I want to make this seo friendly .. I already tried this link
How do I convert a query string to a segment URI? but nothing happened, can someone help me plz
current url localhost/project/home?cat_id=MjY=&cat_type=Mg==
desired url localhost/project/home/team-category
thanks in advance.....
source to share
You are mainly looking for slug based URLs ( Semantic URLs )
So how do you implement a slug?
Explain with an example:
URL - http://www.example.com/products/apple-iphone-5S-16GB-brand-new/
1) Assuming you have a product page and on a weekend product page you need some data from the url to figure out which product to display.
2) Before we access our database with help id
, we get the URL. But now we're going to do the same (accessing our database) by simply replacing id
with slug
, and that's it!
3) So adding an extra column to your database named slug
. Below is 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
...
...
For this, you need 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['/products/(: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>
I've also answered slug earlier. Check if it helps. How to remove params from url.
source to share