Using other packages in my laravel package

I created a package with a workisen artisan team. Everything works fine, but I want to use a different composer package inside my package. What is the best / cleanest way to do this?

+4
source share
1 answer

Basically, you just need to require the package in your composer.json package and create it in your service provider by introducing this package in 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 a package has only static functions, there is not much to do, you probably have to use it directly in your classes:

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

-, , :

class MyRest implements MyClassInterface {

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

}

. , - . :

interface MyClassInterface {

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

}
+1

All Articles