Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

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

 

Posted 16 October 2019 - 08:00 AM #1

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.

 

 



 
  • soft-solid
  • Junior Member
  • Members
  • Join Date: 19-Apr 10
  • 899 posts

Posted 16 October 2019 - 12:23 PM #2

Hello

 

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

Team of SoftSolid
cs-cart.pl

 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11401 posts

Posted 16 October 2019 - 05:30 PM #3

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?


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 

Posted 17 October 2019 - 06:54 AM #4

 

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,
);
}
}


 

Posted 17 October 2019 - 06:58 AM #5

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,



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11401 posts

Posted 17 October 2019 - 05:42 PM #6

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?


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 

Posted 19 October 2019 - 06:42 AM #7

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,



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 11401 posts

Posted 19 October 2019 - 07:33 PM #8

I would do something like:

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

EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 

Posted 20 October 2019 - 10:44 AM #9

 

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.