<p>At the end of the day, it all comes down to <strong>personal preference</strong>. 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).</p>

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

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

<hr>

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

<pre><code>craft-&gt;myPlugin-&gt;myMethod();
</code></pre>

<p>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:</p>

<pre><code>&lt;input type="hidden" name="action" value="myPlugin/myAction"&gt;
</code></pre>

<p>Lastly, the same thinking applies to your main Variable file:</p>

<pre><code>{{ craft.myPlugin.myVariable }}
</code></pre>

<p>Just makes for a cleaner interface for end-users and other devs. :)</p>

CC BY-SA 3.0
2014-07-05T09:41:38.473Z
2014-07-06T02:48:43.073Z
2014-07-06T02:48:43.073Z
Lindsey D
<p>When it comes to naming files within a plugin - is it preferable to follow <code>PluginHandle_SomeController</code>, or stuff everything inside a single <code>PluginHandleController</code> file?</p>

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

<p>Any benefit to one way or the other?</p>

<plugin-development>
Best practices for plugin service/controller/etc file names
# Best practices for plugin service/controller/etc file names

(Asked by: username_0 on 2014-07-05)

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?

**Tags:** plugin-development

---

## 1 answers

### Answer by username_1 on 2014-07-05. Score: 9 | Accepted answer

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:


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


---

