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

[eluser]searain[/eluser]
http://ellislab.com/forums/viewthread/94028/

How to pass parameter to index function?

I found the above thread.

So the default way in the codeigniter to pass the parameter to index function

is ..../somecontroller/index/1, ..../somecontroller/index/2?

If that is the default way, I would use it instead of make set up or configuration changes.

Thanks!
#2

[eluser]danmontgomery[/eluser]
[quote author="searain" date="1264735651"]is ..../somecontroller/index/1, ..../somecontroller/index/2?[/quote]

Yep
#3

[eluser]pistolPete[/eluser]
You could also use the _remap() function, e.g.:
Code:
function _remap($method)
{
    if (method_exists($this, $method))
    {
          $this->$method($this->uri->segment(3));
    }
    else
    {
          $this->index($method);
    }
}

Then you would use ..../somecontroller/1 without the index parameter.
#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 );
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB