Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create seo friendly url with codeigniter?

I am leaning Codeigniter from few days and I am wondering to create blog on Codeigniter but two question are coming from my mind.

1. How to create SEO Friendly URL just like WordPress is using.

2. How to get page content after navigate to that URL.

I have created table tbl_post where I am storing post details and table structure are:

 1. ID
 2. Title
 3. Content
 4. Tags
 5. Status
 6. Create Time
 7. Update Time
 8. Author ID
 9. Status

Now I want to create dynamic post URL from above table.

For example: http://www.example.com/hello-world/

And after navigate to above URL, How to get content of hello-world post?

You have notice that I have not passed any ID to example URL to get content. Any suggestion, If I pass ID and don't want to show in URL string?

That's It.

I will thankful If you guide me to the proper way.

Thanks.


Code Review

Home Page (List view of my blog posts)

Home Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
    
    public function index()
    {
        $this->load->model("HomeModel"); // Load home model
        $data['postData'] = $this->HomeModel->postData(); // Get posts data from postData function
        
        $this->load->view("global_header"); // Include header area
        $this->load->view("home", $data);
        $this->load->view("global_footer"); // Include footer area
    }
    
}

Home Model:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class HomeModel extends CI_Model{
    
    public function postData()
    {
        $this->db->select('tbl_post.*, tbl_user.first_name, tbl_user.last_name');
        $this->db->from('tbl_post');
        $this->db->join('tbl_user', 'tbl_post.author_id = tbl_user.id', 'Left Join');
        $query = $this->db->get();
        return $query->result();    
    }
    
}

Home View

<?php for($i=0; $i<count($postData); $i++): ?>
        <?php 
            $postID =  $postData[$i]->id;
            $postTitle = $postData[$i]->title;
        ?>
        <a href="<?php echo base_url("$postID/$postTitle"); ?>">
            <h2 class="post-title">
                <?php echo $postTitle; ?>
            </h2>
        </a>
    </div>
    <hr>
<?php endfor; ?>

Now my URL looks like this: http://example.com/1/man-must-explore-and-this-is-exploration-at-its-greatest

Domain: http://example.com
ID: 1
Title: man-must-explore-and-this-is-exploration-at-its-greatest

I have created another view(post) to display post content with post ID(ID fetch from URL).

Am I going right way? Need your suggestion to improve my logic.

like image 853
Manan Avatar asked Sep 28 '22 04:09

Manan


1 Answers

Answer for first Question

CodeIgnitor is oop concept framework, which use Model View Controller.(MVC architecture).

So each and every click on site it will goes to Controller. Then only controller will decide what to show or what to do next.

If there are 3 pages (ex: home, product, contact us) then use 3 controllers(home=use default controller, product= use product, contact us = use contact) to it.

So then your URL will be (if you click product) it shows www.test.com/product, if you click Contact it shows www.test.com/contact.

Answer for second Question

IN your table always maintain auto-increment id always(MUST). Then you can pass id to controller and can get data you want.

for ex. i load some content to page.(content on product).

your data showing page(view)

<?php 
foreach ($product as $variable) // $product is data array which you send data
{
?>
   <div class="content">
        <a href="[call your controller here, in my `product`]/[*call sub function*, i use `show`]/ [*then pass here to product id*]<?php echo $variable['id']"></a> 
   <div>
<?php
}
?>

So your URL look like(after click on product, Assume id of that product is 25) www.test.com/product/show/25

then in product controller create

public function show($id)//$id variable to assign value which coming through URL 
{
   1. your code
   2. then load view
}
like image 80
Abdulla Nilam Avatar answered Oct 03 '22 00:10

Abdulla Nilam