PinoyTech.org

CodeIgniter, Kohana, Mootools, jQuery and CSS

Using strings for URI Segments in CodeIgniter

Posted by teejay on April 21, 2010

If you have used Kohana before, you'll notice that their URI::segment() function accepts strings. CodeIgniter's s URI::segment doesn't.

Here's a way to extend the URI class to accept strings.

<?php

class MY_URI extends CI_URI{

 function rsegment($string, $default = FALSE)
 {
  if (ctype_digit((string) $string))
  {
   return parent::rsegment($string, $default);
  }

  if ($key = array_search($string, $this->rsegments) AND isset($this->rsegments[$key+1]))
  {
   return $this->rsegments[$key+1];
  }
  return FALSE;
 }

 function segment($string, $default = FALSE)
 {
  if (ctype_digit((string) $string))
  {
   return parent::segment($string, $default);
  }

  if ($key = array_search($string, $this->rsegments) AND isset($this->rsegments[$key+1]))
  {
   return $this->segments[$key+1];
  }
  return FALSE;
 }
}

Categories: How To

Tags: codeigniter, uri

No Comments

Comments are not allowed