Nisha haridas
Nisha haridas

Reputation: 332

pretty url setting in codeigniter

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?

Upvotes: 0

Views: 1755

Answers (1)

Rooneyl
Rooneyl

Reputation: 7902

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);
}

Upvotes: 2

Related Questions