skip to Main Content

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.

3

Answers


  1. 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
    }
    
    Login or Signup to reply.
  2. I did SEO friendly url for a tour portal, with the following approach that is little similiar to wordpress. Use a slug column in your tbl_post so that you can easily change it leter whenever you want. Lets say you have a post title like

    How to create seo friendly url with codeigniter?

    Then first make the title in lowercase, replace all the spaces and special characters with - so that your slug will looks like this

    how-to-create-seo-friendly-url-with-codeigniter

    Now later when page will load you have to detech the ID to fetch the data of particular post then attach the id in the end of the url how-to-create-seo-friendly-url-with-codeigniter-15 where 15 is the id of the particular post.

    So when you call the main controller that time match the post id and the slug then rendor the related data, otherwise show 404 Error.

    Also modify config/routes.php so that you will get the url like: http://www.example.com/how-to-create-seo-friendly-url-with-codeigniter-15/

    Login or Signup to reply.
  3. Step 1 : You need to add one field “slug” in your table.
    Step 2 : As you will save post you can use your post title as slug as you mentioned hello world. You can replace spaces with “-” sign. So you title should be unique .

    Step 3 : You can pass that slug value in URL instead of post id and can fetch the content on basis of your slug.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search