Welcome Guest, Not a member yet? Register   Sign In
Help Remaping to Index function
#1

[eluser]benners[/eluser]
I currently have to include the index in the URL to pass the parameter to the index function. http://localhost/gallery/index/74

I've tried the follow with http://localhost/gallery/74 and it seems to be ok for the Index function but it doesn't remap to other functions in the Gallery Controller.

Code:
public function _remap($method, $params = array())
{
echo "method: $method<br />";
print_r($params);
$params2 = array_slice($this->uri->rsegment_array(), 0);
print_r($params2);
$method = $params2[0];

if ($method == 'gallery')
{
  $this->index($params2[1]);
} else {
  $this->default_method(); // This does not show default method
}
}
#2

[eluser]InsiteFX[/eluser]
Code:
public function _remap($method, $params = array())
{
     $method = 'process_'.$method;
     if (method_exists($this, $method))
     {
         return call_user_func_array(array($this, $method), $params);
     }
     show_404();
}
#3

[eluser]benners[/eluser]
Could you clarify this code please? I've commented a line below and it works for all methods except if index has a parameter. E.g. http://localhost/gallery/74. (I don't understand the purpose of 'process_')

Code:
public function _remap($method, $params = array())
  {
  // $method = 'process_'.$method;
   if (method_exists($this, $method))
   {
    return call_user_func_array(array($this, $method), $params);
   }
   show_404();
  }
#4

[eluser]gRoberts[/eluser]
This is where "routes" come into play.

Within your routes.php file, you can do

Code:
$route['gallery/(:num)'] = 'gallery/index/$1';
$route['gallery'] = 'gallery/index';

That way, both 'localhost/gallery' and 'localhost/gallery/74' will work.
#5

[eluser]benners[/eluser]
I've changed my routes for Gallery to:

Code:
$route['gallery/(:num)'] = 'gallery/index/$1';
$route['gallery'] = 'gallery';
$route['gallery/(:any)'] = 'gallery/$1';

And I've removed the _remap code and it works fine. It's looks like the remap code is nolonger needed.
#6

[eluser]gRoberts[/eluser]
I'm not sure why you'd use the third line, since gallery is already mapped.

If you had an action called "view" within your gallery file, 'localhost/gallery/view/1' would work fine without your 'gallery/(:any)' route.
#7

[eluser]Aken[/eluser]
Your routes should be in order from most to least specific. Put your (:any) rule above the gallery by itself rule.




Theme © iAndrew 2016 - Forum software by © MyBB