• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RESTful Input

#1
[eluser]riggerthegeek[/eluser]
This script is an extension of the input library to allow parsing of RESTful URLs (one of the more useful features of Zend).

Add to /application/library. You won't need to instantiate this as CodeIgniter loads the Input library by default.

If your URL is like this......

http://www.example.com/controller_name/m...ry2/value2

Then you can get the values of the two queries like this. Usually would be in your controller, but you can do it pretty much anywhere (if you don't care about MVC standards).

Code:
$query1 = $this->input->rest('query1'); // returns "value1" in this example
$query2 = $this->input->rest('query2'); // returns "value2"

It will return false if the query doesn't exists

Code:
$query3 = $this->input->rest('query3'); // returns false

The Library
Code:
<?php
/**
* MY_Input.php
*
* @author Simon Emms <[email protected]>
*/
class MY_Input extends CI_Input {
    final public function __construct() {
        parent::CI_Input();
    }




    /**
     * REST
     *
     * Extends the input class so that you can get
     * RESTful input.  Returns either the value or
     * false
     *
     * @param string $index
     * @param bool $xss_clean
     * @return string/false
     */
    final public function rest($index, $xss_clean = false) {
        $objRouter = &load;_class('Router');
        $objUri = &load;_class('URI');
        
        $arrUri = $objUri->segment_array();

        /* Only pay attention after the method */
        $arrUri = array_slice($arrUri, array_search($objRouter->method, $arrUri));

        $queryId = array_search($index, $arrUri);
        if($queryId !== false) {
            /* The next one along is the REST value */
            $valueId = $queryId + 1;
            if(array_key_exists($valueId, $arrUri)) {
                $value = urldecode($arrUri[$valueId]);
                /* Do we have to clean out the filth */
                if($xss_clean) {
                    $value = $this->xss_clean($value);
                }
                return $value;
            }
        }
        return false;
    }





    /**
     * REST Replace
     *
     * Replaces the REST value in the URL.  Takes the
     * old value and puts the new value in there.
     *
     * @param string $index
     * @param string $value
     * @return string
     */
    final public function rest_replace($index, $value) {
        $original = $this->rest($index);
        if($original !== false) {
            /* Something's already set */
            $objRouter = &load;_class('Router');
            $objUri = &load;_class('URI');

            $arrUri = $objUri->segment_array();

            $queryId = array_search($index, $arrUri);
            unset($arrUri[$queryId]);
            unset($arrUri[$queryId + 1]);

            $url = site_url(implode('/', $arrUri));
        } else {
            /* Nothing set - just add to URL */
            $url = current_url();
        }
        /* We've got the URL less the REST key */
        $url .= '/'.$index.'/';
        $url .= urlencode($value);
        return $url;
    }

    
}
?&gt;


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.