0

I have the url with controller 'package',function 'tour_package' and parameter '1'. http://www.mysite.in/package/tour_packages/1 Here the parameter is set as the id. I need to change it with corresponding url string http://www.mysite.in/package/tour_packages/American

and another example like: http://www.mysite.in/package/tour_packages/2 to http://www.mysite.in/package/tour_packages/African

How to accomplish this?

Nisha haridas
  • 332
  • 1
  • 8
  • 22
  • but you want to change it with a redirect to the new url or you want to use "America" and "Africa" instead of "1" and "2"? – Patroklo Apr 20 '12 at 07:20

1 Answers1

2

First of I would change the url link to use url_title() (url helper). If you create the query using the text value of the ID from what I presume is a database call this will give you something like

(For example, if the record for id 1 is name="America");

echo 'http://www.mysite.in/package/tour-packages/'.url_title($name);

Would give;

http://www.mysite.in/package/tour-packages/American

If you then add this extended router class it will automatically reformat dashes to underscores for you.

<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {

    function set_class($class) {
        $this->class = str_replace('-', '_', $class);
    }

    function set_method($method) {
        $this->method = str_replace('-', '_', $method);
    }

    function _validate_request($segments) {
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).EXT)) {
            return $segments;
        }
        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0])) {       
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            if (count($segments) > 0) {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).EXT)) {
                    show_404($this->fetch_directory().$segments[0]);
                }
            } else {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT)) {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        // Can't find the requested controller...
        show_404($segments[0]);
    }
}

Then you can add an entry in the routes file (in config); something like;

$route['package/tour_package/(:any)'] = "package/lookup_by_name/$1";

You then need to create a method in the package controller called lookup_by_name($name). This method needs to do a sql query to your database where you get the id from the name value. You can then continue to load a view or do whatever you want.

E.g

public function lookup_by_id($name) {
   // sql to get id from database record

   // load the view?
   $this->view($id);
}

public function view($id) {
   // sql to load full record from id
   $this->load->view('foo', $data);
}
Rooneyl
  • 7,802
  • 6
  • 52
  • 81
  • Since it seems to simply replace the ID with name, why not just add a column "slug" in the database and set that to `url_title($name)`. From there on it's just to select on slug instead of ID – danneth Apr 20 '12 at 09:04
  • @danneth Your suggestion is valid (and good), but what is used to select the record is immaterial, my answer just shows a suggested method of using a custom route with lookup. – Rooneyl Apr 20 '12 at 09:12
  • It looks rather neat actually, I would never have thought about it myself. – danneth Apr 20 '12 at 12:10
  • I'm not the one asking the question :) – danneth Apr 20 '12 at 15:48
  • @danneth, sorry got distracted. – Rooneyl Apr 21 '12 at 18:28