Using other packages inside my laravel package [closed]
Asked Answered
K

1

6

I created a package with the artisan workbench command. Everything works fine, but I want to use another composer package inside my package. What's the best/cleanest way to do that?

Krems answered 21/9, 2014 at 17:49 Comment(7)
Can you add some more detail? Did you try something?Hubby
No... that's why Im asking.Krems
Then is question is opinion based and too broad and will hopely get closedHubby
Well the gentleman below understands what I am talking about. Not sure what other information you requireKrems
I also understand what you are talking about, but your question is opinion based (one says do it like this, the other one has another way) and too broad (a complete answer would be impossible long) - and actually my flag was approved as helpfulHubby
Well isn't that the point of this? There probably are multiple ways to aproach this, but I dont know any, so I am asking this here, where people understand way more than me. Yes, the question is short and simple, but I dont understand what other information could this possibly need.Krems
This question is OFF-TOPIC because of the reasons I wrote aboveHubby
S
4

Basically you just have to require the package in your package composer.json and instantiate it in your service provider, injecting that package into your class:

class MyPackageServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->app['mypackage'] = $this->app->share(function($app)
        {
            return new MyPackage(
                new ThirdPartyPackage()
            );
        });
    }

}

And use it in your class:

class MyPackage {

    public function __construct(ThirPartyPackage $package) 
    {
        $this->package = $package
    }

    public function doWhatever() 
    {
        $this->package->do();
    }    
}

If the package has only static functions, there is not much that can be done, you'll probably have to use it directly in your classes:

Unirest::get("http://httpbin.org/get", null, null, "username", "password");

Something you can do is to create a class to dinamically use that package:

class MyRest implements MyClassInterface {

    public function get($url, $headers = array(), $parameters = NULL, $username = NULL, $password = NULL)
    {
        return Unirest::get($url, $parameters, $headers, $username, $password);
    }

}

And use your own class in your package. By not exposing that package directly you can use it dinamically and still be able to change the implementation later. You should also create an interface to for your public methods:

interface MyClassInterface {

   public function get($url, $headers = array(), $parameters = NULL, $username = NULL, $password = NULL);

}
Sundin answered 21/9, 2014 at 17:55 Comment(3)
What do I do if the package Im trying to use has static functions? Im trying to include this package if that helpsKrems
Edited to provide information on statics.Sundin
I actually managed to get it working just by adding use Unirest; to my file;Krems

© 2022 - 2024 — McMap. All rights reserved.