CodeIgniter Forums
Private function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Private function (/thread-43342.html)



Private function - El Forum - 07-08-2011

[eluser]Perkin5[/eluser]
I am developing a blog and I have a controller called blogin associated with a form view.

At the moment, a successful login calls a method admin in another controller called blogadmin. The code is:

Code:
redirect('blogadmin/admin');

and that works fine. However, it's possible to reach that method by entering the address as a url and I want to prevent that. So I rename the admin method as _admin as I understand that this makes it a private method and not available via url.

I then change my code to:

Code:
redirect('blogadmin/_admin');

and I get a 404. Why doesn't it work?


Private function - El Forum - 07-08-2011

[eluser]jmadsen[/eluser]
you already answered it:

"I understand that this makes it a private method and not available via url."


Private function - El Forum - 07-08-2011

[eluser]Perkin5[/eluser]
OK but I do want to call the method admin so how do I do it if not with a redirect?


Private function - El Forum - 07-08-2011

[eluser]boltsabre[/eluser]
You cannot call a private function from another controller, it goes against the whole idea of OOP - you made it private, that means you cannot touch it except from inside the same controller. So move it to the correct contoller and you'll be fine!

If your function is returning something (a value) just call as such to get the value it returns:

Code:
$callMyPrivateFunction = __myPrivateFucntion();

if ($callMyPrivateFunction == 1) // or whatever you want to do with it.

The nature of 'redirect', which your currently doing, is a bit like 'load->view...'. Basically your telling your controller to go to another controller... and if that controller isn't loading a view or anything, what do you expect to display on the screen to the user?


Private function - El Forum - 07-08-2011

[eluser]Perkin5[/eluser]
Oh great - I've got it! I thought private simply meant 'not available via url'. I've moved the function and the underscore works perfectly. Thank you so much - we noobs must seem so stoopid.


Private function - El Forum - 07-08-2011

[eluser]boltsabre[/eluser]
Ha, glad you got it working, and it's okay, I've asked many 'newbie' questions on here as well ;-)