Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can private helper methods still be accessed in views?

Just another "why is it that way" question: I noticed that private helper methods still can be accessed within views. Why's that? And is there a way to prevent this (e.g. when having helper methods that should only be called from within another helper)?

like image 211
Joshua Muheim Avatar asked Oct 11 '12 08:10

Joshua Muheim


People also ask

Can helper methods be used to transfer data from controller to view?

You can put a method in a controller and then call helper_method in the controller to indicate that this method is available as if it were in a helper too. This method will then be available to all controllers and to all views.

Do helper methods have to be private?

Helper methods are generally public and static so that these can be invoked independently. Each methods of a helper class should work independent of other methods of same class.

Why should helper methods be private?

1 Answer. I prefer such helper methods to be private static; which will make it clear to the reader that they will not modify the state of the object.

Can helper methods be public?

Helper methods are often declared as public static so they can be invoked with the class name like Classname.


1 Answers

Helpers are modules that get mixed in to the views. This means that public, protected and private methods in the helper become public, protected and private methods on the views.

I don't think that you can actually hide the helper methods from the view. You'd need to do something like have a helper class which you instantiate in the helper and then delegate calls to that - sounds like it could get messy fast though. :)

like image 63
Shadwell Avatar answered Oct 12 '22 13:10

Shadwell