SilverStripe 3: Can a module extend mysite/code/Page.php?

473 views Asked by At

Good afternoon,

I don't know if what I want to do is possible, so here goes.

I have a module that extends Page_Controller, but I want certain functions to be accessible via the site root.
Eg: getMyDataObjectList();
Currently, they only work if I go through the normal MVC routing structure.

I've found that when I place the function 'getMyDataObjectList' within '/mysite/code/Page.php' it works.

The problem is, I don't want to place the code in there. I want it bundled with my Custom Module, but to work the same as though it was in 'mysite/code/Page.php'

[Example Scenario]

site root: http://[somesite].com
By default, the 'Page.ss' template loads.
I would like the theme developer to be able to call my module functions (API) within any template/Layout page, and have the result returned from the site root
Currently, this only works if I move the "API" functions to '/mysite/code/Page.php'

If the code is in my module, then data is only returned when you go to:
http://[somesite].com/[module_controller]

Can this be achieved? If so, how?

Thanks for your assistance.

[Update - Code Solution]

///>MyExtension.php
class MyExtension extends Extension{
    public function getMyDataObjectList(){
         return 'object list goes here!';
    }
}//class

///>[Module] => _config.php
Object::add_extension('Page_Controller', 'MyExtension');

And as always, I do a (/dev/build?flush=1) just in case. Thanks to: 'simon_w'

1

There are 1 answers

3
AudioBubble On BEST ANSWER

Yes, this is relatively straightforward. Simply, create an Extension subclass with your methods in them and then add that to Page_Controller. An Extension subclass is almost exactly the same as a DataExtension, they're just for classes other than DataObjects.