Fahid Mohammad
Fahid Mohammad

Reputation: 930

How to apply codeigniter's URL_TITLE value to the URL

I'm completely new to ci,

I have a url something like this:

http://localhost/mvc/post/prod_id/1

And I want it to be:

http://localhost/mvc/post/my-best-product

So far I'm able to manage to route all that to home/post/ and learned the segment function also.

But my question is how do I really get the url_title out to the actual url.

I couldn't find any information on this particular subject. All I could find is how to use the url_title and how to route in ci. But they don't explain how we can actually change the base url name.

Please guide me to the right direction.

solution Example:

public function my_method($product_slug)
{
    $product1 = "training-for-recruitment";
    $product2 = "training-for-od";

    if($product_slug==$product1)
    {
        $this->load->view('prod1');

    }else if($product_slug==$product2)
    {
        $this->load->view('prod2');

    }else{
        show_404();
    }
}

This is not what exactly I'm going to do. It is just for others to understand the workaround of Slugs.

Upvotes: 2

Views: 1000

Answers (2)

hsuk
hsuk

Reputation: 6870

Generate a unique slug for each of your product. Add a field for it on product table and every time, while selecting a product from table, use that slug instead of getting the product from primary id.

So, your function becomes like:

function product($product_slug)
{
    //get product by slug from database
    //load view page
}

Now, in config/routes.php

$route['your_controller_name/(:any)'] = "your_controller_name/product/$1";

Upvotes: 4

Rid Iculous
Rid Iculous

Reputation: 3962

You need to do this:

  1. Set up some logic that translates "my-best-product" to "1"
  2. Set up routing in CI that calls your 'prod_id' controller and passes the URI vars

Upvotes: 1

Related Questions