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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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

Page controller extension init()


Go to End


8 Posts   314 Views

Avatar
Faloude

Community Member, 55 Posts

3 April 2017 at 6:18am

Edited: 05/04/2017 11:10pm

Hi all, does someone has a simple page controller extension laying around? Mine doesn't seem to work and I can't figure out why.

<?php

class PageExtension_Controller extends Extension {

    public function init() {
        parent::init();

        // Google maps API
        Requirements::javascript('https://maps.googleapis.com/maps/api/js?key=xxxxxx');

    }

}

// config.yml

Page_Controller:
  extensions:
    - PageExtension_Controller

The JS file in the extension init method is simply not loaded. Am I missing something here?

Avatar
martimiz

Forum Moderator, 1391 Posts

4 April 2017 at 4:35am

Try using contentcontrollerInit() instead of init(). That might work...

Avatar
Faloude

Community Member, 55 Posts

6 April 2017 at 12:33am

I wishfully tried that, no luck unfortunately. Doesn't change anything.

Also I ran a test to check if the controller extension is loaded it all. The extension is loaded just fine. It's just the init method that doesn't execute

Avatar
patjnr

Community Member, 102 Posts

18 April 2017 at 9:24pm

Edited: 27/04/2017 1:14am

Why not use onAfterInit or onBeforeInit.
This code below should work.


<?php

class PageExtension_Controller extends DataExtension{

    public function onAfterInit() {
         Requirements::javascript('https://maps.googleapis.com/maps/api/js?key=xxxxxx');
   }

Avatar
Faloude

Community Member, 55 Posts

22 April 2017 at 4:57am

@patjnr, OnAfterInit() does work, excellent!

This leaves me wondering, why does the normal Init() method not work in extensions? Is it supposed to be working for Extensions at all?

Avatar
martimiz

Forum Moderator, 1391 Posts

22 April 2017 at 5:21am

Great that patjnr helped you out! Leaves me wondering why I couldn't have come up with that. Holes in my head for sure... :(

About init I think I can explain... A DataExtension or Extension is used to add extra methods and properties to its owner. So you can't add init() because the Page_Controler already has that one covered. However Controller::init() provides an extension hook 'onAfterInit', so if you add that method instead, it will be called by the owners init() function.

Avatar
Faloude

Community Member, 55 Posts

22 April 2017 at 6:09am

That makes sense.

I've got so used to overriding methods (getCMSFields, onBeforeWrite etc) that I expected the same pattern in Extension classes

Avatar
martimiz

Forum Moderator, 1391 Posts

22 April 2017 at 6:47am

Yep, the naming is sort of confusing. Standard extended or derived classes on the one side, where you override certain methods, and the SilverStripe Extension class, which is something completely different.

It used to be called a Decorator class, which for me was much less confusing. :) It must have not quite fit the decorator pattern, for it to be renamed...