Search code examples
codeigniter-2

Codeigniter URI routing with for multiple methods


I am working on codeigniter. Controller name is "project" and 4 functions are there.."get_city_data", "get_store_data", "get_currency", "get_description".

Parameters I am passing in above 2 functions are - get_city_data($state, $city), get_place_data($state, $city, $store).

So on browser to render these functions I am using the urls respectively as-

http://localhost/project/get_city_data/state1/city1

I want to change the urls like http://localhost/state1/city1

In routes.php if I define a route like this $route['(:any)/(:any)'] = 'project/get_city_data/$1/$2' then it reflects for all other functions as well but I want to change urls for these 2 functions only.

Also I do not want to use _remap function as it doesn't go well with my needs.

Can anybody help me in this? ThankYou


Solution

  • I got it solved using regex.

    In routes.php i wrote

    $url_source = $_SERVER['REQUEST_URI'];
    if(!(preg_match('%^/project/get_description\?state=[a-zA-Z_-]*%', $url_source) || preg_match('%^/project/get_currency\?state=[a-zA-Z_-]*%', $url_source))){
    
            if(!preg_match('%^\/[a-zA-Z_-]+\/[a-zA-Z_-]+\/[a-zA-Z_-]+$%', $url_source)){
                    $route['(:any)/(:any)'] = 'project/get_city_data/$1/$2';
            }else{
                    $route['(:any)/(:any)/(:any)'] = 'project/get_store_data/$1/$2/$3';
            }
    }
    

    This way I was able to route for particular functions.