0

I want to get the developer name from Plugin.php using the getDeveloper() method so I created a function devName() in Variable.php

public function devName() {
    return craft()->crmPortal->getDeveloper();
}

but when i want it to print it in my template, using this {{craft.crmPortal.devName}} I'm having this Error. Property "Craft\WebApp.crmPortal" is not defined.

  • Two things - you're using devName and getName ... and as I mentioned, it may be a case sensitivity issue - what's the actual class name of your plugin that you've defined? And you've installed your plugin in setting right? – Jeremy Daalder Apr 25 '17 at 8:44
  • Yup ! i installed it in plugin settings. the class name in my CrmPortalPlugin.php is => class CrmPortalPlugin – lance adrianne dela cruz Apr 25 '17 at 8:55
  • Craft\CrmPortalService and its behaviors do not have a method or closure named "getDeveloper". I'm getting this error right now – lance adrianne dela cruz Apr 25 '17 at 8:58
  • Yes, because you have called the function devName() above...so use .devName in your twig code. All this stuff is very name sensitive. – Jeremy Daalder Apr 25 '17 at 8:59
  • do i need to right something in services ? – lance adrianne dela cruz Apr 25 '17 at 9:01
3

It sounds like you're attempting to access the getDeveloper method on your plugin's main service class, but the class doesn't exist.

You have two options:

  1. Create the service class, and implement a getDeveloper method.
  2. Call the getDeveloper method on your main plugin class (which I suspect is what you're really trying to do).

Option One: Service

Your minimal service class will look like this:

<?php

namespace Craft;

class CrmPortalService extends BaseApplicationComponent
{
    /**
     * Returns the developer name.
     *
     * You could either hard-code the developer name, or you could
     * retrieve it from the main plugin class, as described in
     * option two.
     *
     * @return string
     */
    public function getDeveloper()
    {
        return 'John Doe';
    }
}

Option Two: Plugin

Craft uses Services to expose functionality, in exactly the same way that plugins do. If you're trying to figure out how to do something, just look for a Craft Service with a similar name.

In this case, you want to retrieve your main plugin file, so the PluginsService is a good place to start. We're in luck, it has a getPlugin method, which accepts a plugin handle, and returns the plugin class.

Here's how you might use that, to call the getDeveloper method in your main plugin class.

craft()->plugins->getPlugin('crmPortal')->getDeveloper();
| improve this answer | |
  • I want to access the getDeveloper() method in the CrmPortalPlugin.php and print it into the screen – lance adrianne dela cruz Apr 26 '17 at 0:53
  • What I did was I create a function developerName() in Variable.php and put the craft()->plugins->getPlugin('crmPortal')->getDeveloper();. And i used it in my template to print it on the screen using {{ craft.crmPortal.developerName() }} – lance adrianne dela cruz Apr 26 '17 at 0:59
  • Good to hear you got it sorted. If you haven't already done so, I recommend having a read through the plugin development documentation, to understand how the various elements of a plugin work together. – Stephen Lewis Apr 26 '17 at 1:01
  • Thank you sir . Can you also give me an advice on what to read if i want to make an http request in craft cms? – lance adrianne dela cruz Apr 26 '17 at 1:09
  • I take it you mean an HTTP request from your plugin to an external site (such as an API)? If so, you should take a look at Guzzle (version 3 is a bit out of date, but it's what Craft uses internally, so it's easier to just stick with that). – Stephen Lewis Apr 26 '17 at 1:14

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.