views:

65

answers:

2

Hello!

Question about controllers. Can controller call it`s own class methods inside an action?

EDIT: Oh sorry. I meant I dont want to repeat myself. :)

+2  A: 

Unless you are using some obscure framework preventing this somehow, you should be able to call methods from within a class/instance, regardless of the class/instance being a Controller or something else.

If you find yourself writing code that needs to be reused across multiple controllers, you could consider writing a BaseController and move common functionality into this one or -better imho- separate common functionality into helper classes, which you can then instantiate and call on demand. Basically, if you need something in all controllers, move it to a BaseController. If you need it in some controllers, make it a helper. If you only need it in one specific controller, keep it there.

Gordon
I`m just trying to find the best practices for doing PHP stuff. :)
PPPHP
A: 

Why not?

It's all up to you, but I really don't see the reason to avoid DRY ( LOL ). The sense of MVC is to have separated controller / model / view, nothing else :)

EDIT: Still yes, controller can call it's own methods inside of actions. Good practice would be to give action methods names like action_something or something_action, and have a 'base' controller which you'd extend later. Like;

Controller
Controller_Application extends Controller
Controller_Layout extends Controller_Application

where the first one would be some kind of 'abstraction' (framework-oriented) and others would take care of application logics. So later you'd have ...

Controller_Forum extends Controller_Layout
Controller_News extends Controller_Layout
Controller_Ajax extends Controller_Application

depending on what you want to use certain controller for.

It's all up to you; you can put your own methods where ever you want to and call them from where ever you want to :)

Kemo
cool, we submitted at exactly the same time. That should give a badge :D
Gordon
yeah, doesn't happen really often :)
Kemo