Using an Existing Library in Symfony

I am new to symfony. I am trying to use the pre-existing library that I used for my payment gateway API with Symfony (v2.3).

Before using Symfony, I will have the composer.json file in the root directory and I will just use PHP require 'vendor/Braintree... However, with Symfony, it’s very difficult for me to use the library for the payment gateway API by importing it into my controller.

Note. I use Entity in the same controller as in the previous example, which has a similar directory structure and works fine:

use Jets\JetsBundle\Entity\Company;

Here is what I tried to use the payment gateway API:

 use Jets\JetsBundle\Braintree\braintree\braintree_php\lib\Braintree; 

and Braintree.php contains:

namespace Widb\WidbBundle\Braintree\braintree\braintree_php\lib;

I keep getting the following error:

FatalErrorException: Error: Class 'Jets\JetsBundle\Controller\Braintree_Configuration' not found in C:\xampp\htdocs\www\symfony\src\Jets\JetsBundle\Controller\DefaultController.php line 239

And the line DefaultController.php Line 239 contains:

Braintree_Configuration::environment('sandbox');

As a side note, I did nothing but drag and drop the finished custom library directory from my old server to the Symfony directory on the new server. Is there any script or cmd configuration missing or something else?

I appreciate any help in this regard. I will always be grateful, someone can help me fix this problem.

Here is my DefaultController.php code:

http://pastebin.com/kwisEBzL

Thank you very much in advance!

+6
source share
3 answers

This Braintree project seems to be PSR-0, so you can easily use it.

Do not do that:

 use Jets\JetsBundle\Braintree\braintree\braintree_php\lib\Braintree; 

There is no such namespace.

Enable the Braintree project using Composer (note that they already have a PSR-0 startup configuration: https://github.com/braintree/braintree_php/blob/master/composer.json ).

EDIT : add this to your composer.json and don't forget to run composer update (or php composer.phar update )

Update 2: As indicated in the comments, it is recommended to use a stable tag / branch. That way, if changes happen, you can fix them before the update. Therefore, always check the library version (2.23 may not be the latest version).

 "require": { ... "braintree/braintree_php": "~2.23" }, 

The autoloader should select all classes if you use the global namespace (note the leading slash):

 \Braintree_Configuration::environment('sandbox'); 

Let me know if this works.

Note that you should probably create a set that configures for you in a centralized location.

+9
source

You really missed one of the first comments .. that was in place and is likely to kill a lot of headaches.

Follow the instructions at https://github.com/cometcult/CometCultBraintreeBundle , this is the package that Braintree provides. The package in this case is actually a β€œmodule” into which you can connect and configure the configuration files.

Relevant commands that are missing for composer processing:

Once you're done, you should take a look at the bottom two parts, in addition to what is still dry.

 $factory = $this->get('comet_cult_braintree.factory'); $customerService = $factory->get('customer'); 

If you need more help let me know.

+2
source

I do not understand what you are actually trying to do in your DefaultController.php , but I have some assumptions.

UPD:

FIRST and SECOND .

THIRD:

When you try to use Jets\JetsBundle\Braintree\braintree\braintree_php\lib\Braintree; , you only load the Braintree class from the Braintree.php file. Not the entire file with other data, for example require_once .

FOURTH:

/vendor The symfony2 project directory usually contains the third part libraries. So you can put lib here.

We hope that some of these points will help you.

PS Sorry for the bad English.

+1
source

Source: https://habr.com/ru/post/956451/


All Articles