Skip to content

Instantly share code, notes, and snippets.

@Eclarian
Created November 8, 2011 17:12
Show Gist options
  • Save Eclarian/1348406 to your computer and use it in GitHub Desktop.
Save Eclarian/1348406 to your computer and use it in GitHub Desktop.
CodeIgniter URI - Access Associative Items as URI Object Properties
class MY_URI extends CI_URI {
/**
* Config
*
* This allows the URI class to assign and access the config object
* without overloading the object
*
* @since 2.0.0
*/
public $config;
/**
* Constructor
*
* @access public
*/
public function __construct()
{
parent::__construct();
}
// -------------------------------------------------------------------------
/**
* Get Magic Method
*
* This method is used to fetch parameters from the URI to Assoc
*
* EXAMPLE
* $this->uri->id1 >> Looks for /id1/value/ within the URI
*
* @param string Name of the key one wants to fetch
* @return mixed FALSE when not found
*/
public function __get($name)
{
// Check if URI Already Cached
if( ! isset($this->keyval[3]) ) // "3" is the default URI to Assoc that Eclarian Uses
{
// Can ALWAYS use the ROUTED uri_to_assoc because it falls back
// to segments when the routed segments aren't available
$this->ruri_to_assoc();
}
// Check if $name is in the array
if( ! isset($this->keyval[3][$name]))
{
$this->$name = FALSE;
return FALSE;
}
// Save the Property to the Object so it will be cached
// This way it will not have to go through the magic methods processing next time
$this->$name = $this->keyval[3][$name];
// Send back the Object Property
return $this->$name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment