Welcome Guest, Not a member yet? Register   Sign In
pass parameter to index function
#4

[eluser]wasil[/eluser]
hi all,

i wanted to use dash-separated URIs even for 'method' segment.

Saiful's solution gave me an inspiration, but i wanted something more universal.

here is set of two methods ( method itself is called from the separated _call method since i use it for other remapping purposes ).

i'll appretiate any improvement hints.


Code:
/**
     * @param string $method
     * @return void
     */
    function _remap( $method ) {
        // grab uri segments
        $uri_segments = $this->uri->segment_array();
        // remove controller and method
        $uri_segments = array_splice($uri_segments,2);
        // call method
        $this->_call ( $method, $uri_segments );
    }
    
    /**
     * universal method caller. supports dash-separated URIs
     * @param string $method
     * @param array $segments
     * @return void
     */
    protected function _call ( $method, array $segments = null ) {
        // try to translate dash to underscore when method not found
        if ( ! method_exists($this,$method) ) {
            $method = strtr ($method,"-","_");
        }
        // method found
        if ( method_exists($this,$method) ) {
            // render uri segments as method arguments
            $arguments = is_array($segments) ?  "\"" . implode ( "\", \"", $segments ) . "\"" : "";
            // process command
            eval ( "$"."this->".$method." ( ".$arguments.");");
        // method not found
        } else {
            // render error page
            show_404();
            // or optionally go on index page
    //        $this->index ( $method );
        }
    }


Messages In This Thread
pass parameter to index function - by El Forum - 01-28-2010, 03:27 PM
pass parameter to index function - by El Forum - 01-28-2010, 03:36 PM
pass parameter to index function - by El Forum - 01-28-2010, 05:59 PM
pass parameter to index function - by El Forum - 02-19-2010, 06:36 AM



Theme © iAndrew 2016 - Forum software by © MyBB