6

When it comes to naming files within a plugin - is it preferable to follow PluginHandle_SomeController, or stuff everything inside a single PluginHandleController file?

This is probably a subjective question for the most part - but in building out a plugin at the moment, I have 4 different Service files, and I'm not sure if I'm being too pragmatic by breaking each different service type out individually, or if I should just include it within the single file. Each service isn't complex, they're basically a function or two each, but cover different aspects of the plugins feature set.

Any benefit to one way or the other?

1 Answer 1

9

At the end of the day, it all comes down to personal preference. Depending on how large and/or complex each of your Service files is, it may make sense to break them up into separate files. Or if you've got less than a dozen service methods total, you could easily combine them into a single service file (if you choose).

For me personally, I try to break it up by where those service methods are being called from. MyPlugin_VariableService and MyPlugin_ControllerService for example... and any methods that are shared would go in the "main" MyPluginService file.

But every developer (and project) is different. You may have some other method of organizing that's better suited to you. So mostly personal preference.


However, the one thing I would definitely suggest... If you build any service methods which you'd like to be triggered from other plugins, then you may want to put those in your main service file. So the other plugins could simply call it like this:

craft->myPlugin->myMethod();

As far as an API for your plugin goes, it's much cleaner. Similarly, for any Controller actions which submit through a front-end form, you may want to have those submit through the "main" controller:

<input type="hidden" name="action" value="myPlugin/myAction">

Lastly, the same thinking applies to your main Variable file:

{{ craft.myPlugin.myVariable }}

Just makes for a cleaner interface for end-users and other devs. :)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.