Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

DataObjectDecorator for Page_Controller init()?


Go to End


3 Posts   3824 Views

Avatar
ChrisBryer

Community Member, 95 Posts

3 December 2009 at 5:02pm

how can i use dataobjectdecorator to override a Page_Controller's init method?

I'm trying this, but it isnt overriding init.. (it will override index() though)

class PodcastController extends DataObjectDecorator {

	public function init(){
		Debug::dump('testing init call');
	}
}

_config.php:
...
DataObject::add_extension('Page_Controller', 'PodcastController');
...

thanks alot,
-Chris

Avatar
Sean

Forum Moderator, 922 Posts

3 December 2009 at 9:17pm

Edited: 03/12/2009 9:22pm

I think what you may need to do is extend from Extension, not DataObjectDecorator. Also, I notice you've called it a PodcastController. Semantically speaking, this doesn't make sense. It would probably be something more like PodcastPageExtension.

Try defining onBeforeInit() or onBeforeInit() on your extension class that is applied to Page_Controller.

I think onBeforeInit() and onAfterInit() are only in 2.4. However, you could apply this patch if using SS 2.3: http://open.silverstripe.org/changeset/78035/modules/sapphire/trunk/core/control/Controller.php

In your _config.php, do the following instead as well:

Object::add_extension('Page_Controller', 'PodcastController');

Just out of interest, what are you trying to achieve with the extension to Page_Controller?

Sean

Avatar
ChrisBryer

Community Member, 95 Posts

5 December 2009 at 9:35am

Edited: 05/12/2009 9:36am

Hi Sean,
thanks for your answer.

out of curiosity, is there anything wrong with overriding index() instead of init()?

what i am doing is building a podcast module which essentially contains a dataobject that holds information for each episode, and gets attached to pages through a many_many relationship table. i considered making a podcast page, but i wanted to investigate extending pre-existing classes and overriding init() to add the podcast rss feed so itunes users can subscribe.

thanks again,
-Chris