Hey guys,
@da², @fliwire, thank you for your help. It pushed me in the right direction.
I got my things done and wanted to share with whoever would find that useful.
Here was my need: a specific folder to store some classes, usable easily across templates (ie "use Class" and not "include ...".
These classes wouldn’t fit (IMO) into a DefaultPage class for separation of concerns matter.
And I didn’t want to try to develop a module, seemed an overkill and time is running...
I decided to store my custom classes files into some subfolders of the /site/classes folder. At first, I used a /site/src folder (and it worked), but as the classes folder already exists, I thought it made more sense to use this folder. This is my architecture
├── site/
│ ├── assets
│ ├── classes/
│ │ ├── Repository/
│ │ │ ├── AbstractRepository.php
│ │ │ └── ContactRepository.php
│ │ ├── DefaultPage.php
│ │ ├── HomePage.php
│ │ └── Utils.php
│ ├── modules
│ └── templates
└── wire
For autoloading use PW autoloader, in put in init.php
$classLoader = $wire->classLoader;
$classLoader->addNamespace("ProcessWire", __DIR__ . '/classes');
Then the magic happens. My ContactRepository.php, for example :
namespace ProcessWire\Repository;
use ProcessWire\Utils;
class ContactRepository extends AbstractRepository
{
// my stuff here...
}
and when I need it in a template file :
namespace ProcessWire;
use ProcessWire\Repository\ContactRepository;
$contactRepo = new ContactRepository;
$speakers = $contactRepo->getAllSpeakersAndModerators();
Remarque:
In the init.php file, I tried with and without
$classLoader->addSuffix("Repo", __DIR__ . '/classes/repositories'); // does not change anything
$classLoader->addPrefix("Repo", __DIR__ . '/classes/repositories'); // does not change anything
I can't really grasp in which situation this useful, and the doc is quite short... 😅
So with this file structure, I can really get a step further in logic handling and code maintenance.
Hope someone will find this useful.
Cheers