Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

joedawson's avatar

Install Package Dependencies

Hello all,

I've just started to develop a package I'm working on - it'll require some dependencies. I've specified these on my packages composer.json.

How can I install these so I can start using them with my package?

Thanks!

0 likes
10 replies
RachidLaasri's avatar

Run composer install on your package folder, but don't forget to add the vendor folder to the .gitignore file.

joedawson's avatar

Hey @RachidLaasri, thanks for your reply.

I tried that earlier but it didn't appear to work at that time - so I assume I had done it wrong.

So I'm requiring guzzlehttp/guzzle with my package, I then did a composer install which installed them in a vendor folder in my package (now ignored as you recommend). I can definitely see the package in there now.

In my package, I have a file named AmazonProducts of which I added the use to the top for guzzle.

// Location
// /packages/Dawson/AmazonProducts/src/AmazonProducts.php

namespace Dawson\AmazonProducts;

use GuzzleHttp\Client;

class AmazonProducts
{
    protected $client;

    public function __construct()
    {
        $this->client = new Client;
    }

    public function search($query)
    {
        $res = $this->client->request('GET', '/api/products');

        return $res;
    }
}

Just as a tester, I'm trying to make a GET request on an API route that already exists in my project. But I'm getting an error saying GuzzleHttp\Client does not exist.

Any ideas why it isn't working? I've also tried a composer dump-autoload too (in my package folder).

joedawson's avatar

Sure can @RachidLaasri - here's my packages composer.json.

{
    "name": "dawson/amazon-products",
    "description": "Package description.",
    "license": "MIT",
    "authors": [
        {
            "name": "Joe Dawson",
            "email": "email@email.com"
        }
    ],
    "autoload": {
        "psr-4": {
            "Dawson\\AmazonProducts": "src/"
        }
    },
    "minimum-stability": "dev",
    "require": {
        "php": ">=5.5.0",
        "illuminate/support": "~5.1",
        "guzzlehttp/guzzle": "~6.0"
    }
}

And here's my packages structure:

Structure

joedawson's avatar

Adding the following line to to the top of my AmazonProducts.php has worked - but surely I shouldn't be having to do this?

require(__DIR__ . '/../vendor/autoload.php');
1 like
kingpabel's avatar

There is a difference for run composer command.Please ensure first go to your package folder directory where your composer.json file exists,then run composer update.Not on your project root.

joedawson's avatar

@kingpabel I've already done that (look at my previous replies) - found a solution in the end, which I'll leave as a reply in a moment.

joedawson's avatar
joedawson
OP
Best Answer
Level 18

I solved this by doing the following.

In my apps main, composer.json I added the following repository - which is the relative path to my pacakge itself.

Using my package's name, I added it to my required dependencies and ran a composer update - which then worked.

"repositories": [
    {
        "type": "path",
        "url": "packages/Dawson/AmazonProducts"
    }
],
"require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.1.*",
    ...
    "dawson/amazon-products": "dev-master"
},

Please or to participate in this conversation.