• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Private function

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:


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:


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

you already answered it:

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

OK but I do want to call the method admin so how do I do it if not with a redirect?

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:

$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?

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.

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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.