PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] How don't break MVC and DRY, sharing a method (Helper/Controller)

 April 22, 2022     cakephp, cakephp-2.3, controller, helper, php     No comments   

Issue

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?


Solution

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.



Answered By - gpedote
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing