skip to Main Content

My current URL is like:

www.hostname.com/get_city/

(this url is already shorten by using routes)
Here get_city is my method name and now what i exactly want is

1)remove controller name from url

2)pass selected city value from dropdown and set in URL as a parameter

So,Required url is: www.hostname.com/california OR www.hostname.com/newjersey

NOTE:I know how to use routes but in such case how to make dynamic URL?!
AND please don’t give me direct reference of ellislab docs because i have already tried those things

3

Answers


  1. For dynamic route in codeigniter:

    try like this:

    in your routes.php file copy and paste these codes:

    require_once (BASEPATH . 'database/DB' . EXT);
    require_once (BASEPATH . 'helpers/url_helper' . EXT);
    require_once (BASEPATH . 'helpers/text_helper' . EXT);
    $db = &DB();
    
    $query = $db -> get('news');
    $result = $query -> result(); //result return all records
    foreach ($result as $row) {
        $string = rawurlencode(str_replace(' ', '-', strtolower($row -> subject)));
        $route[$string] = "controller/news_details/$row->id";
    }
    

    so you can , change $string with any string that you want.

    then try type new url and see Routes will work fine.

    NOTE:.htaccess file must be removing index.php in url

    hope this help.

    Login or Signup to reply.
  2. You can try with the method “_remap”.

    Check the official documentation for more info about the behavior of the function:

    http://www.codeigniter.com/user_guide/general/controllers.html

    Regards.

    Login or Signup to reply.
  3. You need to config routes in application/config/routes.php

    After existing routes add

    $route['([a-z]+)'] = 'controller_name/method_name/$1';
    

    But this overwrite all routes and before this route you need to declare all routes for you controllers

    $route['product/:any'] = 'product/$1';
    $route['catalog/:any'] = 'catalog/$1';
    

    and after

    // this route be used when previous routes is not suitables
    
    $route['([a-z]+)'] = 'controller_name/method_name/$1'; 
    

    DOCS:

    http://code-igniter.ru/user_guide/general/routing.html

    CI2: URI Routing

    CI3: URI Routing

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