I have a couple of generic functions that i use across multiple different pages and DataObjects. I'm trying to figure out the best place to store these. Currently I have them in Page.php but as they don't apply to all pages, I would like to move and store them in classes in a sub folder of the mysite/code called helpers. This will also help the next developers to locate and update these classes if need be. (I am OK with leaving it in the Page.php class if that is the way it is done in silverstripe)
<?php
public class GetPublishedArticles {
public function GetAllPublishedArticlesByType($class){
error_log('Hit GPA',0);
$pages = $class::get();//->filter(array('Published'=>1));
if($pages->count()){
return $pages;
}
else{
return false;
}
}
}
Template:
<% loop GetAllPublishedArticlesByType('Page') %>
$Link
<% end_loop %>
Things I have tried.
- Adding them to mysite/code/helpers folder, running a dev/build/flush=all but no matter what the functions are never hit. Even if I type gibberish anywhere in there it doesn't throw a php error
- Adding them to mysite/code folder. Running a dev/build/flush=all force some changes, and typing gibberish in here causes a php error on dev build. Cannot access from the template still
- I tried adding the class GetPublishedArticles to the $has_one array on Page.php - still nothing.
- Adding the function directly to page.php. - this works as expected.
So how can i make use of this re-usable code? Adding it to the page works, but might make it difficult for other developers to locate if they are trying to change.