2

I'm not sure if I missed documentation on it, but is there a method to follow when including additional plugin files for services, controllers, variables etc.?

For bigger plugins it'd help to do this, something like

- /myplugin
   -/services
      -MyPluginService.php
      -MyPlugin_OtherService.php
      -MyPlugin_AnotherService.php

I've tried doing this and being sure to follow the service structure of:

namespace Craft;

class MyPlugin_OtherService extends BaseApplicationComponent
{

  public function testing()
  {
      return "success!";
  }

}

But when trying to access it from say the myPluginVariable.php file, I get the CException error that the property is not defined.

return craft()->myPlugin_OtherService->testing();

Is there a step I'm missing? Do I need to declare these extra files somewhere so that they are included?

2

When calling services, you should not include "service" in method name.

Try return craft()->myPlugin_other->testing();

  • Oops, I knew that. Mistake when typing up question! I still get "Property "Craft\WebApp.myPlugin_other" is not defined." – taylor Mar 4 '15 at 20:17
  • @Zack I think the methods are case-sensitive, so double check spelling mistakes. – carlcs Mar 4 '15 at 20:25
  • I've tried all lowercase, camel, upper camel, not having any luck. By default Craft will include all files inside /service right? Or do I need to declare the added files? – taylor Mar 4 '15 at 20:50
  • Just tried in the service file and it worked. I was getting the error when trying to call craft->myPlugin_Other->testing(); in the myPluginVariable.php file. Are the extra service files only accessible from the main service file? – taylor Mar 4 '15 at 20:52
  • No, I have plugins with multiple service files and they work fine. I wonder if Craft defaults to the MyPluginService file if it exists. Can you try renaming that one to MyPlugin_MainService.php and seeing if that allows you to call other services? – Aaron Berkowitz Mar 4 '15 at 20:54
2

Adding my own discovery in case anyone ever runs into this. The format that worked was lower camel for both the plugin name, and also lower camel for the secondary name.

So for the following class:

class MyPlugin_OtherService extends BaseApplicationComponent
{
    public function myfunction() {}
}

I was able to access it like:

return craft()->myPlugin_other->myfunction();

For a longer name, like:

class MyPlugin_MoreFunctionalityService extends BaseApplicationComponent
{
    public function myfunction() {}
}

The formatting should look like:

return craft()->myPlugin_moreFunctionality->myfunction();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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