How To Import And Use An Existing Class From A Different Addon In My Addon?

Hello all,

I am in the middle of making a module which requires to import a payment method class into my current addon. The one I am working on is an extension of API.

The payment method class is sitting in the original addon. I do not want to copy the same class into my folder. I want to import the same functionality to mine.

The problem is every time when I do import using namespace import it gives 500 server error. And it is very difficult to identify the exact issue bcos its an API.

But the same class if I have a copy in my addon folder with my class then it works fine. This is not what I want to do.

Please advice how to resolve this.

Thanks in advance.

Hello

How do you import this class? Show code
If you enable devel mode and show errors, what do you get?
Best regards
Robert

I believe (guess) that the addon you are referencing is either not installed or disabled. Hence when the namespace is referenced, the higher level access controls within cs-cart are failing since by cs-cart standards, that class should NOT be loaded. The easiest thing to do is to in fact copy the class to your addon and adjust the namespaces.

If the addon is in fact enabled (active) then why not just reference the class having your own 'use' operator to identify the source of the class?

Hello

How do you import this class? Show code
If you enable devel mode and show errors, what do you get?
Best regards
Robert

Hi soft-solid,

Thank you for your reply,

Here are the classes,

Class1 is in myaddon1.

MyApi is an API class which I develop now.

when I call MyApi from postman I get only 500 status. but no idea what the error is. It would be great if you can tell me how to capture those API errors for debugging. May be how to do log errors?

class 1
Path : app\addons\myaddon1\class1.php
namespace Tygh\Addons\myaddon1;
class Class1 {
public $var;
public function class1fun(){
$this->var = "from class 1 funct";
return $this->var;
}
}
API class
namespace Tygh\Api\Entities;
use Tygh;
use Tygh\Api\AEntity;
use Tygh\Api\Response;
use Tygh\Registry;
use Tygh\Addons\myaddon1\Class1;
class MyApi extends AEntity{
public function index($id = '', $params = array())
{
$status = Response::STATUS_OK;
$obj1 = new Class1();
$data = $obj1->class1fun();
return array(
'status' => $status,
'data' => $data
);
}
public function create($params)
{
return array(
'status' => Response::STATUS_METHOD_NOT_ALLOWED,
'data' => $data
);
}
public function update($id, $params)
{
return array(
'status' => Response::STATUS_METHOD_NOT_ALLOWED,
'data' => array()
);
}
public function delete($id)
{
return array(
'status' => Response::STATUS_METHOD_NOT_ALLOWED,
'data' => array()
);
}
public function privileges()
{
return array(
'index' => true,
'create' => false,
'update' => false,
'delete' => false,
);
}
}

I believe (guess) that the addon you are referencing is either not installed or disabled. Hence when the namespace is referenced, the higher level access controls within cs-cart are failing since by cs-cart standards, that class should NOT be loaded. The easiest thing to do is to in fact copy the class to your addon and adjust the namespaces.

If the addon is in fact enabled (active) then why not just reference the class having your own 'use' operator to identify the source of the class?

Hi tbirnseth,

Thanks for your reply.

The addon is installed. And as you mentioned I copied this class into my api folder and doing the development. But I believe that it would be much better if I could 'use' the class from original folder itself. I belie it would be helpful in the long run.

Regards,

Hi tbirnseth,

Thanks for your reply.

The addon is installed. And as you mentioned I copied this class into my api folder and doing the development. But I believe that it would be much better if I could 'use' the class from original folder itself. I belie it would be helpful in the long run.

Regards,

You can certainly use an existing class if the addon it is contained within is active.

Regarding yor 500 error... What are the paths to your 2 classes relative to your app/addons/myaddon1 directory?

You can certainly use an existing class if the addon it is contained within is active.

Regarding yor 500 error... What are the paths to your 2 classes relative to your app/addons/myaddon1 directory?

Hi,

Pardon for my late reply,

Here are the class paths,

I want to access Class1 methods from MyApi class.

\app\addons\my_api\Tygh\Api\Entities\MyApi.php

\app\Addons\myaddon1\Class1.php

I also would like to know how to capture exact errors while working with API classes.

Regards,

I would do something like:

if( !class_exists('Class1') )
  include(Registry::get('config.dir.addons'')."/myaddon/Class1.php");

I would do something like:

if( !class_exists('Class1') )
  include(Registry::get('config.dir.addons'')."/myaddon/Class1.php");

Hi tbirnseth,

This works for me.

Thanks for the tip.