CodeIgniter Forums
Controllers must have an index() function? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Controllers must have an index() function? (/showthread.php?tid=8073)

Pages: 1 2


Controllers must have an index() function? - El Forum - 05-04-2008

[eluser]myerman[/eluser]
I just went through about 2 hours of line-by-line inquiry on a new controller that I added to an existing project. What I thought would be a very easy, very quick upgrade has turned into a lot of grief, but I think I might have stumbled on the solution.

Basically, I had a controller with two functions, create() and view(). There is no way for the user to edit or delete the category of data they are workign with, they can only simply create new one line notes and view them in detail.

well, I kept getting 500 Server Errors every time I linked to admin/attempts/create or admin/attempts/view. I checked for everything. Finally, out of sheer desperation, I renamed the view() function to index().....

....and everything worked just fine.

I just wanted to make double-sure as it appears to my muddled brain that all controllers MUST HAVE a index() function?
Anyone else have any thoughts on this?


Controllers must have an index() function? - El Forum - 05-04-2008

[eluser]dione[/eluser]
i say controllers can have no index function...

i remember ive been in the same problem with view() function...
ive been bugged with this error for days..

please check your php version... we have reserved function names that we cannot use if were running php4...

function view() is a reserved function for php4.. see here http://ellislab.com/codeigniter/user-guide/general/controllers.html#reserved

hope this helps..


Controllers must have an index() function? - El Forum - 05-05-2008

[eluser]Référencement Google[/eluser]
Quote:I just wanted to make double-sure as it appears to my muddled brain that all controllers MUST HAVE a index() function?
Anyone else have any thoughts on this?

It's not a MUST but I would recommend that every of your controllers have this index function because when you call by url the controller using a url like http://yoursite.com/yourcontroller/ , it will be the default function called.

If you don't need to put code inside the index function, just do something like:

Code:
function index()
{
    $this->another_function();
}

function another_function()
{
    // Code here
}



Controllers must have an index() function? - El Forum - 05-05-2008

[eluser]wiredesignz[/eluser]
Or, you can use the _remap() method.

The issue is more about not using reserved names for methods or classes.


Controllers must have an index() function? - El Forum - 05-05-2008

[eluser]Référencement Google[/eluser]
Quote:Or, you can use the _remap() method.

Can you explain how to use it and a quick example?


Controllers must have an index() function? - El Forum - 05-05-2008

[eluser]wiredesignz[/eluser]
CI first checks for a _remap() method before calling the controller, if it exists the URI method segment is passed to _remap(), otherwise the index() method is called and passed the data segment.

See: system/codeigniter/Codeigniter.php - line 210

This allows you to "remap" the method calls to your controller to other methods.


Controllers must have an index() function? - El Forum - 05-05-2008

[eluser]Référencement Google[/eluser]
I didn't know this one, undocumented like a lot of small things like that.
So I would just add in a controller something like:

Code:
function _remap()
{
    $this->myfunction();
}

function myfunction()
{
    // Code here
}

Then I don't need the index() function, is that right?
But what's happend in this case while I call http://mysite.com/mycontroller/myfunction ?


Controllers must have an index() function? - El Forum - 05-05-2008

[eluser]wiredesignz[/eluser]
Yes basically you have it right. But it has much more potential.
For instance:
Code:
http://domain.tld/blog/this-is-blog-post

//can be remapped like so
class Blog extends Controller

function _remap($post_title)
{
    $data['posts'] = $this->db->getwhere("posts_title = '{$post_title}'");
    
    $this->load->view('posts', $data);
}



Controllers must have an index() function? - El Forum - 05-05-2008

[eluser]Derek Allard[/eluser]
Remapping is documented, but as the code grows, I fully concede that its hard to keep everything neatly organized. If you encounter items in CI that are undocumented, please feel free to post about it on the bug forum.


Controllers must have an index() function? - El Forum - 05-05-2008

[eluser]Référencement Google[/eluser]
That looks cool thanks, I have to give it a try.
I just can't figure out yet in the real world where I can use this. In witch kind of situations do you use something like that Wiredesignz?