Part of the EllisLab Network
   
 
Parameters to Controller index()
Posted: 13 November 2009 04:56 PM   [ Ignore ]  
Grad Student
Rank
Total Posts:  50
Joined  07-11-2008

What is the easiest way/most automated way to send data from the URL to the index function of a controller so that other methods still work?

I’d prefers something that doesn’t require me to list out all of my methods. Also, I’d like to avoid messing with routes.php.

The scenario is basically…

class Post extends Controller
{

index
($ID)

write()

edit($ID)

etc.
Profile
 
 
Posted: 13 November 2009 06:08 PM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2774
Joined  07-27-2006

_remap() baby! I rarely write a controller without one.

class Post extends Controller
{

function _remap($method)
{
  
if (method_exists($this$method))
  
{
    $this
->$method();
  
}
  
else {
    $this
->index($method);
  
}
}

index
($ID)

write()

edit($ID)

etc.

You could make it more robust (like pass all URI params to index()) but that’s the gist.

 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

Profile
 
 
Posted: 16 November 2009 11:27 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  50
Joined  07-11-2008

Thanks for your response.  That is close but darn _remap doesn’t accept more than one argument and also breaks the standard URI segment passing (e.g. function foo($arg1, $arg2) ).

What I really want is for URL http://fake-domain.com/controller/one/two/three/etc to check if one is a method.  If yes, act completely normally 100% (without having to use $this->uri->segment(n) ). If not, act as if “index/” had been inserted before “one”.

Profile
 
 
Posted: 17 November 2009 12:09 AM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2774
Joined  07-27-2006

Very easy solutions to those problems (that’s what I meant by making it more robust).

So, voila!:

function _remap($method)
{
  $param_offset 
2;

  
// Default to index
  
if ( ! method_exists($this$method))
  
{
    
// We need one more param
    
$param_offset 1;
    
$method 'index';
  
}

  
// Since all we get is $method, load up everything else in the URI
  
$params array_slice($this->uri->rsegment_array(), $param_offset);

  
// Call the determined method with all params
  
call_user_func_array(array($this$method), $params);
 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

Profile
 
 
Posted: 17 November 2009 12:19 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  50
Joined  07-11-2008

OMG Thanks!  You are the coolest!

Profile
 
 
Posted: 17 November 2009 12:23 AM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2774
Joined  07-27-2006

No problem. Clear, concise questions and requests here typically get good responses. So, thank you for being a good asker.

 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

Profile
 
 
Posted: 17 November 2009 02:47 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  1
Joined  11-17-2009

Thanks.

Profile
 
 
Posted: 17 November 2009 05:07 AM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  17
Joined  10-29-2009

wow this is really cool.. I could use this one on my project.. smile

 Signature 

Hunk Cute Programmer

Profile