tags:

views:

64

answers:

1

Hello! I am trying to refactor my Catalyst application and I would like to introduce a common base class for my controllers. This base controller would load some data and put some other data into the stash for every request. I have got some trouble getting to the stash. Simple solution would be to implement a default auto action in the base controller. This works, but I have to remember to always call the super auto in the derived controllers. This is easy to forget, is there a better solution? In other words: Is there a simple way to tap into the request processing that wouldn’t be so easy to break in the derived controllers?

+3  A: 

I don't think you need inheritance in order to accomplish your goal. You might have other reasons why inheritance is a good idea for your application, but it seems as a rule that inheritance is generally overused when other methods of class composition would be more appropriate.

In particular for this case, Catalyst provides for this functionality by allowing you to specify an auto method in your Root controller, which will always be invoked before the auto methods of your more particular controllers for every request. No inheritance required.

Adam Bellaire
You’re right, the code I’d like to refactor lives in the root controller now. I want to move it, because (a) I’d like to keep the common controller code separate from the code of the title page and other main parts served by the Root controller, and (b) I’d really like to have some data available as methods on `$self` instead of going through the `$c->stash->…` hoops all the time.
zoul
@zoul: That's fine, but it's working in the opposite direction from the framework, where you're expected to put common stuff in the root controller. If you want to separate out "main" and "title page" stuff, why not make *that* a seperate controller, and forward the request processing out of Root when appropriate? That would separate your code the way you want, though it doesn't give you a shortcut for `$c->stash->` ...
Adam Bellaire