Drop Shadow
Drop Shadow

Reputation: 808

How to get pretty URL in Codeigniter?

I have a product controller (Codeigniter) where I load 40 categories in my index function. When I click on a category, I want to load all item of that particular category. To do that, I can easily write a function to load all that items.

Function load_item($categoryId’)
{
 // in short    
 …… where categoryId = ‘$categoryId’
 // then load it to view 
}

Then I have URL like /product/load_item. But I want URL like /product/laptop or /product/desktop (/product/category_name). So, it's not possible to write 40s function for every category and also its not optimal solution. I don’t want to change anything in index function. Have you any idea please???

Upvotes: 0

Views: 97

Answers (2)

Abdulla Nilam
Abdulla Nilam

Reputation: 38642

Method 01

In routes.php

$route['category/(:any)'] = 'category/load_item';

Output - www.example.com/category/(value/number-of-category)


Method 02

In View URL passing method(Means <a>)

<a href="<?php echo base_url()?>category/item/<?php echo category_code?>/<?php echo category_code?>">Click me</a>

so in controller

Function item($catgoryName, $categoryId)
{
   // in short    
   where categoryId = '$categoryId';
   // then load it to view 
}

Output - www.example.com/category/item/laptops/1

Upvotes: 0

RiaanV
RiaanV

Reputation: 258

You have to setup the routes for the url's so under your config folder go to routes and then create a route like so

$route['product/(:any)'] = 'catalog/product_lookup';

You can find all relevant information in the Codeiginter User Guide

Upvotes: 1

Related Questions