Jump to content

Best Practice: Repository


AndreasWeinzierl
 Share

Recommended Posts

Hey there,

I am currently building an API with Process Wire. Because the retrieval of data (although using pages) is a little complex, I would like to separate this from the template. 

Is it a good idea to, for instance, put kind of a repository class into into the the modules directory?

Would there be a better option? Should I also derive from "Module" and add a module ending?

Link to comment
Share on other sites

9 hours ago, AndreasWeinzierl said:

Is it a good idea to, for instance, put kind of a repository class into into the the modules directory?

Would there be a better option?

Nothing wrong with this approach. You can always build your own, site-specific modules for such features, and as long as the module has "autoload" set to "false" ProcessWire won't load it unless you really need it – i.e. unless you specifically request it via a template file, a bootstrap script, etc.

A "bare-bones" alternative would be separating this logic into a regular PHP file, which you then include as needed.

Which approach makes most sense tends to depend a lot on the complexity and your use case, and even then it's ultimately a matter of preference ?

9 hours ago, AndreasWeinzierl said:

Should I also derive from "Module" and add a module ending?

If you do go with a module approach, you'd probably want to use something like this:

<?php namespace ProcessWire;

class MyModule extends WireData implements Module {
  ...
}

You can use .module or .module.php as a file suffix. ProcessWire supports both, but as far as I can tell .module is still more commonly used ?

Link to comment
Share on other sites

Okay, I guess I do not need to include a module, however, I do not get it to work. It alsways says Class ProcessWire\Classname not found in my file.

However, if I rename a already available module, e.g. helloworld, I also cannot access it anymore? What does happen there? Where do I have to flip the bits?

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