Vipul sharma
Vipul sharma

Reputation: 1255

slug is not working properly in codeigniter

I am creating pages with slug in codeigniter. But it is not working if my slug in database have slashes.

like -:

1) con_r/this //not working

2) con_q //working

3) con_r/this/that //not working

Take a look at my code

routes.php

$route['default_controller'] = "home";
$route['404_override'] = 'home';
$route['translate_uri_dashes'] = TRUE;

require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get('slugu');
$result = $query->result();
foreach( $result as $row ){
    $route[ $row->slug ] = 'page/call/'.$row->slug;
}

Page //controller

 public function call($slug){

    $data['page_base'] = $this->site_model->get_page_info($slug);

    /*call slider*/
    echo $this->call_slider($slug);

    if (empty($data['page_base'])){
        show_404();
    }

    print_R($data['page_base']);
    die;
}

Upvotes: 0

Views: 515

Answers (1)

James Walker
James Walker

Reputation: 795

This is due to the way CodeIgniter handles URL's. The first two URL segments is the class and function. The third and any more segments are passed into the function as variables. So in your example the following URL works because the full slug is passed into the function:

example.com/page/call/con_q

public function call($slug) {
  echo $slug; // con_q
}

Whereas the following does not because only the first segment of the slug is passed into the function

example.com/page/call/con_r/this

public function call($slug) {
  echo $slug; // con_r NOT con_r/this
}

public function call($segment_1, $segment_2) {
  echo $segment_1; // con_r
  echo $segment_2; // this
}

A solution which I think would work would be the following. Please note that this is untested and may need some modifications.

example.com/page/call/con_r/this

public function call() {
  $slug = func_get_args();
  $slug = implode('/', $slug);
  echo $slug; // con_r/this
}

Upvotes: 1

Related Questions