AndreasWeinzierl

Best Practice: Repository

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?

Share this post


Link to post
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 🙂

Share this post


Link to post
Share on other sites

Thanks for your reply.

I think I am gonna go with the module approach.

 

Do I have to include it, too? Probably, since I cannot access it easily.

How would I add it to composer to autoload it appropriately? I guess psr-4 is not possible?

Share this post


Link to post
Share on other sites

Furthermore, does one module always represent one class with modulename = class name? Should each individual module have an own folder = modulename? 

Did I miss an example where all this is shown? 

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

Update: After putting the files into the directory one still has to perform an installation (via the admin interface -> other possibilities?)

So access problem solved.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.