Tuesday, February 1, 2011

Re: SecondController extends FirstController extends AppController: not supported? --psybear

Thanks, guys (especially to Tilen for his constructive feedback on my
posting habits ;-) ).

It seems to me I often run into limitations of CakePHP that I hope I
can't be the only one who faced them yet and that there's a well known
work-around already. In fact, I often seem to be the only person who
had the need for them yet, and that's sometimes a bit surprising to
me, because I'm definitely not creating very high-end-featured
websites with CakePHP yet, and in those cases I'm just interested in
knowing the reason why those features aren't available in CakePHP (for
which I'm sure there are diligent thoughts behind these decisions).
I'm just the kind of person who's not satisfied with only the fact
that something IS or ISN'T, but rather is interested in WHY IT IS or
ISN'T.

So to get back to my question I'd really like to know WHY
__mergeVars() only supports "pseudo inheritance" from AppController
and doesn't do it e.g. recursively through the whole inheritance-chain
down to AppController. Maybe it's a performance issue? Maybe it's
planned for a next release? Sadly the inline documentation of CakePHP
codes isn't very exhaustive which is pitty because THERE's the place
to put such "business decisions" when there's no other place for such
informations (or maybe I haven't found this resource yet?).

Anyway, no offense to anybody. I don't want to bother anyone or strain
this mailing list, so if my questions really don't fit here I'm happy
somebody's telling me this and I will think about what I could change
in my habits so everybody is happy. I'm using forums and similar
communities for quite a long time now, and I never had problems of any
sort, but maybe things run a little different here. Thanks, Tilen, for
subtly pointing me to this problem with your comment. ;-)

On Tue, Feb 1, 2011 at 4:04 AM, Ryan Schmidt <google-2010@ryandesign.com> wrote:
> On Jan 31, 2011, at 08:24, psybear83 wrote:
>
>> I'm refactoring an application with some controllers and wanted to
>> "divide" them into a group X (that needs some components) and a group
>> Y that needs some other components. My idea:
>>
>> class AppController {
>>  var $components = array('Html', 'Form');
>> }
>>
>> class XController extends AppController {
>>  var $components = array('X1', 'X2');
>> }
>>
>> class YController extends AppController {
>>  var $components = array('Y1', 'Y2');
>> }
>>
>> Now every other controller should inherit from XController or from
>> YController like this:
>>
>> class SomeXController extends XController {
>>  var $components = array('Bla');
>> }
>>
>> I expected XController now to have loaded the following components:
>> Html, Form, X1, X2, and Bla. But sadly it doesn't have all of them! It
>> seems CakePHP doesn't allow controller inheritance over more than only
>> 1 level. Is this true? Is there any known work-around?
>
> From a purely PHP standpoint, your XController's var $components is overwriting your AppController's var $components, and so on. Not knowing about __mergeVars() which Jamie talked about, I'd have suggested you implement a constructor in each of your classes, which calls the parent constructor and then takes care of merging your newly-desired components into the existing components array. PHP has some array functions that should do the trick.
>
>
>
> --
> Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
>
>
> To unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
>

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: