3

I'm working with laravel 5 and trying to integrate the following package: exacttarget/fuel-sdk-php

I executed on my project:

composer require exacttarget/fuel-sdk-php

So I had on my vendor dir exacttarget provider.

First thing I've noticed this particular package doesn't use namespaces, so it still calls require directives but not "use \path\namespace" Is it a right approach? I haven't seen many packages yet but among my past experience doesn't look to me the right approach to write a package...

After this I edit condif/app.php to use ET_Client class.

 'providers' => [
 ...
 'ET_Client',
 ...

],

Once I did this, I got an error: looks like Laravel frmwk tries to instantiate the class, that needs some parameters to work, even if I'm not yet using it (istantiating). It this a normal behavior from Laravel?

Am I missing something ?

koalaok
  • 5,075
  • 11
  • 47
  • 91
  • Looking at the package, isn’t that an SDK for the FuelPHP framework rather than Laravel? – Martin Bean Aug 06 '15 at 15:02
  • Thanks Martin, but where do I check on packagist that a package is made for a particular framework? Is it something you understand only by analyzing the code? – koalaok Aug 06 '15 at 15:05
  • I don’t know what ExtractTarget is. Is Fuel a name of one of their products? – Martin Bean Aug 06 '15 at 15:15
  • They call FuelSDK their bunch of classes in order to interact with their REST Service... I dont think it could be related to FuelPHP – koalaok Aug 06 '15 at 15:18

1 Answers1

2

The providers array is for registering service provider classes. Unless ET_Client extends Laravel’s base ServiceProvider class, it’s not going to work.

Instead, just add the use statements to your PHP classes as and when you need to use the class:

<?php

namespace App\Http\Controllers;

use ET_Client;

class SomeController extends Controller
{
    public function someAction()
    {
        // Instantiate client class
        $client = new ET_Client;

        // Now do something with it...
    }
}
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • That's the solution I guess, but since I have lots of classes.It wont be nice to have to list all classes i need to use in every controllr... I wished i could have them namespaced and then call : use MyNamespace... – koalaok Aug 06 '15 at 15:15
  • Unfortunately you’re at the mercy of the third party. If they haven’t name-spaced their SDK then there’s not a lot you can do. Maybe contact them and ask them to name-space it? Namespaces have been out a good while and is commonly used now. – Martin Bean Aug 07 '15 at 10:38
  • Maybe I'll go for rewriting it namespaced. Just another hint from you. If now I rewrite everything using namespaces. It's not mandatory that i will need to inherit from ServiceProvider, and then register it to config/app.php. I can also simply namespace it ande then use the name space in my controllers. Right? Or do you think that the Serviceprovider way could be more elegant in a Laravel styling view? – koalaok Aug 07 '15 at 11:17
  • I’m not sure I follow? What do you mean by is it mandatory to inherit from `ServiceProvider`? If you’re writing a Laravel service provider class then it’ll need to extend the abstract one, yes. – Martin Bean Aug 07 '15 at 12:36
  • I have this library "fuelSDK" that basically doesn't seem to follow PSR rules neither is written to be easly integrated in laravel. Even if it's on packagist and has it's composer.json file. The question is ... I have bunch of classes working together, a library, which is the best approach to integrate it in Laravel 5 and have it autoloaded or used? Hope I'm clear. thanks – koalaok Aug 07 '15 at 15:33
  • The package vendor determines how classes are auto-loaded (for example: https://github.com/jdillick/FuelSDK-PHP/blob/Dot9/composer.json#L13). As you’ve found, just because a package is on Packagist, doesn’t mean it also follows PSR conventions in terms of coding style or name-spacing. – Martin Bean Aug 07 '15 at 15:43