Gazley Posted August 12, 2012 Share Posted August 12, 2012 Hi there, I created a static helper class for some utility functions (gwutils.php). When I try and access it: $summaryImg = gwUtils::GetImageByCategory('summary', $summary); PW is picking this up through spl_autoload_register and obviously throwing an error because it isn't registered. What is best practice around creating/registering/loading site based custom classes? Thanks! Link to comment Share on other sites More sharing options...
Soma Posted August 12, 2012 Share Posted August 12, 2012 Not sure what exactly you're doing but you could create a modules that is autoloaded and provides functions to use everywhere in PW. Something like this: class HelpersModule extends WireData implements Module { /** * getModuleInfo is a module required by all modules to tell ProcessWire about them * * @return array * */ public static function getModuleInfo() { return array( 'title' => 'Helper Functions', 'version' => 100, 'summary' => '', 'href' => '', 'singular' => true, 'autoload' => true ); } public function init() { $this->setFuel("helpers", $this); } public function getPosts() { return wire("pages")->get("/posts/")->children(); } } Then you can do in your templates: $posts = $helpers->getPosts(); There's variations of this but it should be simple to do. Also you could also make it not autoload and load it on demand. $helpers = $modules->get("HelpersModule"); // load module $posts = $helpers->getPosts(); Edit: wow everytime I edit the post the indentation get lost, that's new to me and pretty annoying... 2 Link to comment Share on other sites More sharing options...
Gazley Posted August 12, 2012 Author Share Posted August 12, 2012 Hey Soma, Thanks for pointing me towards a module for this requirement. I can see me creating lots of modules once I get the hang of it! BTW, the tips you gave me for filtering my image categories have worked really well! PW is so powerful Best, --Gary Link to comment Share on other sites More sharing options...
Soma Posted August 12, 2012 Share Posted August 12, 2012 You should get into modules pretty soon! They're easy as pie and powerful! PS: Glad to hear it worked out, thanks for mention it. Link to comment Share on other sites More sharing options...
ryan Posted August 12, 2012 Share Posted August 12, 2012 I agree with Soma's reply. Also wanted to mention you could always register your own autoload function too. In my case, I either go the route of including a "tools.inc" file at the top of my templates that has all my shared functions (and/or classes if needed), or I use the module route as Soma mentioned. Link to comment Share on other sites More sharing options...
Gazley Posted August 12, 2012 Author Share Posted August 12, 2012 The file extension is .module - will PW get upset if it has a PHP extension too? My IDE doesn't give me the usual "help" if the file hasn't a PHP extension! Link to comment Share on other sites More sharing options...
ryan Posted August 12, 2012 Share Posted August 12, 2012 PW uses the .module extension for the same reason Drupal does... so that the file can quickly be identified (by PW's module loader) as a file containing a plugin module rather than some other PHP file. We've had a request to make it also support .module.php, so will likely be adding this shortly in the dev branch. But for now my suggestion would be to set your editor to recognize .module as a PHP file. For instance, I use VIM and place this in my .vimrc file: set nowrap syntax on au BufRead,BufNewFile *.module set filetype=php 1 Link to comment Share on other sites More sharing options...
Gazley Posted August 12, 2012 Author Share Posted August 12, 2012 I've created the module (based on Soma's example) and placed it in site/modules (with a .module extension only). However, it doesn't seem to get loaded. Is there something you have to do to tell PW to load site based modules? Thanks. Link to comment Share on other sites More sharing options...
Gazley Posted August 12, 2012 Author Share Posted August 12, 2012 Hah, what a Muppet - you have to install it through the Module's panel! Woot! I'm having (many) Senior Moment(s) 1 Link to comment Share on other sites More sharing options...
Gazley Posted August 12, 2012 Author Share Posted August 12, 2012 @ryan - added .module to my PHP pattern and all is well, IDE and more importantly me ... happy again 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now