سؤال

First question on StackOverflow (I don't know if I'm doing this right, so sorry if I mess it up)

Background: My controller has a method that list all actions in my app and plugins called getActions().

And I need to use getActions() in a Helper that overrides HtmlHelper->link() method, because the purpose of this method is return null if the link is forbidden by Acl, thus not rendering any link that would lead to a forbidden action.

Question How to share this method? I'm doing this wrong?

Lib is the right way to go?

This doesn't seem right: In CakePHP, how do I call an AppController method from a helper?

هل كانت مفيدة؟

المحلول 2

The right place for the method is Lib, because its a general purpose method that is going to be used in differents places with differents objectives.

CakePHP's core has many examples of this approach like: CakeTime, CakeNumber and others.

Others examples could be seen in some of CakeDC plugins too.

IMHO @yossi was right about "assuming the data as static", but his approach of storing the data in Session just would make my code more confusing.

In my particular case I'm taking the Lib way. Thanks for the help.

نصائح أخرى

Don't share the method, share its output. as mentioned in the link to the other question.

I assume that this data is static, meaning: if it will be called from any part in the application, it will return the same value.

Call the method in the controller, store its output in a view-variable or in the session, and write a helper that will access this information and do the acl logic.

It won't break the MVC and will avoid DRY.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top