Jump to content

Class Loading


Gazley
 Share

Recommended Posts

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

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... :D

  • Like 2
Link to comment
Share on other sites

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

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

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
  • Like 1
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...