chrizz Posted July 22, 2019 Share Posted July 22, 2019 I have a module (autoload: true) which uses a custom class. If it would be only relevant for templates, _init.php would be the way to go but due to autoload functionality I need the class also to be availabel in the backend - this is where _init.php stops working. If the class is declared in config.php it works fine - but is this the recommended way or is there a better solution for this? Link to comment Share on other sites More sharing options...
Edison Posted July 22, 2019 Share Posted July 22, 2019 Hi @chrizz, personally I load all my custom classes through an autoloader called in config.php. For this purpose you can also consider ready.php. However If the custom class is associated only to a specific module (it will be used only when this module is installed) I think you may consider to load it from within the module itself in init() or ready(). Link to comment Share on other sites More sharing options...
bernhard Posted July 23, 2019 Share Posted July 23, 2019 You can assign your own API variables easily: // MyModule.module.php class foo { public function bar() { return 'works!'; } } class MyModule extends WireData implements Module { public static function getModuleInfo() { return [ 'title' => 'MyModule', 'version' => '0.0.1', 'summary' => '...', 'autoload' => true, ]; } public function init() { $this->wire->set('foo', new foo()); } } You could also just save this instance to $config->foo $this->wire->config->foo = new foo(); 4 Link to comment Share on other sites More sharing options...
chrizz Posted July 23, 2019 Author Share Posted July 23, 2019 @bernhard, thats what I did as well - but then I ran into trouble because I needed to access the same class from a different module (autoloaded as well). And then it became tricky because I don't know any way to control the order of autoloading ? Link to comment Share on other sites More sharing options...
bernhard Posted July 23, 2019 Share Posted July 23, 2019 Then you asked the wrong question ? https://github.com/processwire/processwire/commit/f0cc6f1134e7c7ea5e99ed851862f612996de0c4 1 1 Link to comment Share on other sites More sharing options...
dragan Posted July 23, 2019 Share Posted July 23, 2019 29 minutes ago, chrizz said: And then it became tricky because I don't know any way to control the order of autoloading I never had to deal with it myself, but check out Ryan's announcement. 1 Link to comment Share on other sites More sharing options...
chrizz Posted July 25, 2019 Author Share Posted July 25, 2019 haha. obviously.... the wrong question... yes ?♂️ Thanks a lot for pointing me into the right direction 1 Link to comment Share on other sites More sharing options...
teppo Posted August 17, 2019 Share Posted August 17, 2019 Hey @chrizz. I'm moving this thread to the "Module/Plugin Development" area of the forum. Please post all module development related questions there – the main "Modules" area is only intended for support threads of existing modules. Thanks! 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