ActiveCampaign / activecampaign-api-php

MIT License
115 stars 76 forks source link

PHP Fatal error: Uncaught Error: Class 'ActiveCampaign' not found #97

Closed kaushalsutharhs closed 6 years ago

kaushalsutharhs commented 6 years ago

I'm using Magento2.2.3

I've installed this module using Composer.

But when I try to use this $ac = new ActiveCampaign($this->api_url, $this->api_key); in my module I'm getting following error.

[Tue Mar 27 16:59:47.624276 2018] [:error] [pid 9831] [client 127.0.0.1:44552] PHP Fatal error: Uncaught Error: Class 'Htia\Ronaldsync\Observer\ActiveCampaign' not found in project/app/code/Htia/Ronaldsync/Observer/exportUpdateCustomer.php:41\nStack trace:\n#0 project/vendor/magento/framework/Event/Invoker/InvokerDefault.php(72): Htia\Ronaldsync\Observer\exportUpdateCustomer->execute(Object(Magento\Framework\Event\Observer))\n#1 project/vendor/magento/framework/Event/Invoker/InvokerDefault.php(60): Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod(Object(Htia\Ronaldsync\Observer\exportUpdateCustomer), Object(Magento\Framework\Event\Observer))\n#2 project/vendor/magento/framework/Event/Manager.php(66): Magento\Framework\Event\Invoker\InvokerDefault->dispatch(Array, Object(Magento\Framework\Event\Observer))\n#3 project/generated/code/Magento/Framework/Event/Manager/Proxy.php(95): Magento\Framework\Event\Manager in project/app/code/Htia/Ronaldsync/Observer/exportUpdateCustomer.php on line 41

So how can I use this module in my custom module?

bartboy011 commented 6 years ago

Hey @kaushalsutharhs, the issue is that you're invoking the class in a namespaced class without the leading back slash. You'll need to update your code like this:

$ac = new \ActiveCampaign($this->api_url, $this->api_key);
kaushalsutharhs commented 6 years ago

@bartboy011 I've made the change as you said, But I'm still getting the same error.

bartboy011 commented 6 years ago

@kaushalsutharhs Could you include the new stack trace please?

kaushalsutharhs commented 6 years ago

Hi @bartboy011, Is following useful for you?

[Tue Mar 27 20:15:04.331074 2018] [:error] [pid 3694] [client 127.0.0.1:51182] PHP Fatal error: Uncaught Error: Class 'Htia\Ronaldsync\Observer\ActiveCampaign' not found in app/code/Htia/Ronaldsync/Observer/exportUpdateCustomer.php:41\nStack trace:\n#0 vendor/magento/framework/Event/Invoker/InvokerDefault.php(72): Htia\Ronaldsync\Observer\exportUpdateCustomer->execute(Object(Magento\Framework\Event\Observer))\n#1 vendor/magento/framework/Event/Invoker/InvokerDefault.php(60): Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod(Object(Htia\Ronaldsync\Observer\exportUpdateCustomer), Object(Magento\Framework\Event\Observer))\n#2 vendor/magento/framework/Event/Manager.php(66): Magento\Framework\Event\Invoker\InvokerDefault->dispatch(Array, Object(Magento\Framework\Event\Observer))\n#3 generated/code/Magento/Framework/Event/Manager/Proxy.php(95): Magento\Framework\Event\Manager in app/code/Htia/Ronaldsync/Observer/exportUpdateCustomer.php on line 41, referer: http://jm2.local/index.php/customer/account/create/

public function execute(\Magento\Framework\Event\Observer $observer)
{   
    $ac = new \ActiveCampaign($this->api_url, $this->api_key);
}
bartboy011 commented 6 years ago

@kaushalsutharhs It looks like the same issue is popping up here... What version of PHP are you on? Could you post the whole class (with confidential info redacted)?

kaushalsutharhs commented 6 years ago

PHP Version 7.0.28-1+ubuntu16.04.1+deb.sury.org+1 Magento Version: 2.2.3

<?php
namespace Htia\Ronaldsync\Observer;

class exportNewCustomer implements \Magento\Framework\Event\ObserverInterface
{
    protected $_storeManager;
    protected $_subscriber;
    protected $_logger;

    public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Newsletter\Model\Subscriber $subscriber,
    \Htia\Ronaldsync\Logger\Logger $logger)
    {

        $this->_storeManager = $storeManager;
        $this->_subscriber= $subscriber;
        $this->_logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {   
        $ac = new \ActiveCampaign($this->api_url, $this->api_key);
    }
}
bartboy011 commented 6 years ago

(A cleaner version for future readers)

<?php
namespace Htia\Ronaldsync\Observer;

class exportNewCustomer implements \Magento\Framework\Event\ObserverInterface
{
  protected $_storeManager;
  protected $_subscriber;
  protected $_logger;

  public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Newsletter\Model\Subscriber $subscriber,
    \Htia\Ronaldsync\Logger\Logger $logger
  )
  {

      $this->_storeManager = $storeManager;
      $this->_subscriber= $subscriber;
      $this->_logger = $logger;
  }

  public function execute(\Magento\Framework\Event\Observer $observer)
  { 
      $ac = new \ActiveCampaign($this->api_url, $this->api_key);
  }
}
kaushalsutharhs commented 6 years ago

@bartboy011 Hi thanks for cleaner version but was it useful for you? any solution?

bartboy011 commented 6 years ago

@kaushalsutharhs Here's what I've done to try and recreate this issue:

index.php:

<?php

use Test\Test;

require __DIR__ . '/vendor/autoload.php';

new Test();

Test.php:

<?php

namespace Test;

class Test
{
    public function __construct()
    {
        new \ActiveCampaign('something.api-us1.com', 'KEY');
    }
}

I'm using PHP 7.0.26 to run this script and do not see the error you were seeing. I only see your error if I remove the leading backslash on \ActiveCampaign().

Can you try running composer dumpautoload? I'm not confident it'll work but I'd like to eliminate some possible issues.

bartboy011 commented 6 years ago

@cristiangrama do you have any thoughts on this?

cristiangrama commented 6 years ago

Something is off since using the backslash should not put the ActiveCampaign class in the same namespace as the current file

[Tue Mar 27 20:15:04.331074 2018] [:error] [pid 3694] [client 127.0.0.1:51182] PHP Fatal error: Uncaught Error: Class 'Htia\Ronaldsync\Observer\ActiveCampaign' not found in app/code/Htia/Ronaldsync/Observer/exportUpdateCustomer.php:41\nStack trace:\n#0 

There might some other issues with the code that we cannot tell by looking at just that trace and class. I am wondering if the magento config might have something to do with this.

@kaushalsutharhs I suggest posting your composer.json.

kaushalsutharhs commented 6 years ago

@bartboy011 Thanks but even after running composer dumpautoload I'm getting the same error. @cristiangrama Here is my composer.json

{
    "name": "magento/project-community-edition",
    "description": "eCommerce Platform for Growth (Community Edition)",
    "type": "project",
    "version": "2.2.3",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "require": {
        "magento/product-community-edition": "2.2.3",
        "composer/composer": "@alpha",
        "tig/postnl-magento2": "^1.3",
        "mollie/magento2": "^1.2",
        "activecampaign/api-php": "^2.0"
    },
    "require-dev": {
        "phpunit/phpunit": "~6.2.0",
        "squizlabs/php_codesniffer": "3.2.2",
        "phpmd/phpmd": "@stable",
        "pdepend/pdepend": "2.5.2",
        "friendsofphp/php-cs-fixer": "~2.2.0",
        "lusitanian/oauth": "~0.8.10",
        "sebastian/phpcpd": "2.0.4"
    },
    "autoload": {
        "psr-4": {
            "Magento\\Framework\\": "lib/internal/Magento/Framework/",
            "Magento\\Setup\\": "setup/src/Magento/Setup/",
            "Magento\\": "app/code/Magento/"
        },
        "psr-0": {
            "": [
                "app/code/"
            ]
        },
        "files": [
            "app/etc/NonComposerComponentRegistration.php"
        ],
        "exclude-from-classmap": [
            "**/dev/**",
            "**/update/**",
            "**/Test/**"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Magento\\Sniffs\\": "dev/tests/static/framework/Magento/Sniffs/",
            "Magento\\Tools\\": "dev/tools/Magento/Tools/",
            "Magento\\Tools\\Sanity\\": "dev/build/publication/sanity/Magento/Tools/Sanity/",
            "Magento\\TestFramework\\Inspection\\": "dev/tests/static/framework/Magento/TestFramework/Inspection/",
            "Magento\\TestFramework\\Utility\\": "dev/tests/static/framework/Magento/TestFramework/Utility/"
        }
    },
    "minimum-stability": "stable",
    "repositories": [
        {
            "type": "composer",
            "url": "https://repo.magento.com/"
        }
    ],
    "extra": {
        "magento-force": "override"
    }
}
kaushalsutharhs commented 6 years ago

@bartboy011 @cristiangrama Hi, Any suggestion or update to resolve this issue? We've have tried in fresh Magento Installation but still, it has the same error.

bartboy011 commented 6 years ago

@kaushalsutharhs Could you try something for me? The develop branch of this repo will be released soon as v3.0.0. It has a lot of breaking changes from v2.x, but since you're working with a fresh install, they won't affect you. I'm curious if using that branch which is designed to be purely a Composer package might work better for you. Update your composer.json to use the dev-develop version of our package ("activecampaign/api-php": "dev-develop") and then run composer install. You can read the updated README on the develop branch to follow any additional installation steps. Let us know if that works better for you!

kaushalsutharhs commented 6 years ago

@bartboy011 Hi thanks for the advice.

We have used your suggested branch and in class, we are using following.

use ActiveCampaign\Api\V1\ActiveCampaign;

bartboy011 commented 6 years ago

@kaushalsutharhs Sorry for the slow response, I was out for a few days. Is everything now working?

kaushalsutharhs commented 6 years ago

@bartboy011 Yes, Everything working fine now by making changes noted in my last comment. Thanks for your support :)

bartboy011 commented 6 years ago

@kaushalsutharhs Great! I'm glad it's solved!