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

daveb2's avatar

Composer/autoload problem

Hi all, I'm stuck on a composer/autoloader problem and I wonder if anyone can offer any suggestions.

php 8.1.10 Laravel 9.41.0

I had to make changes to the shweshi OpenGraph package (https://github.com/shweshi/OpenGraph) to make it work without allow_url_fopen(), so I have forked it (this is new to me) and included the local fork in my composer.json as a local repository like so:

"repositories": [
        {
            "type": "path",
            "url": "./local/open-graph",
            "options": {
                "symlink": true
            }
        }
}

Inside the local open-graph repository's composer.json at /local/open-graph/composer.json I have:

{
    "name": "open-graph/open-graph",
    "description": "Fork of shweshi/OpenGraph with fixes to work without allow_url_fopen",
    "type": "library",
    "license": "MIT",
    "autoload": {
        "psr-0": {
            "OpenGraph": "src/"
        }
    },
    "minimum-stability": "dev",
    "require": {}
}

composer validate checks out OK and I can do a composer update of the local repository and my laravel project, and the following completes successfully:

cd local/open-graph/ && echo "open-graph" && composer clear-cache && composer dump-autoload && cd ../.. && echo "laravel" && composer clear-cache && dump-autoload
open-graph

Result:

open-graph
Cache directory does not exist (cache-vcs-dir):
Cache directory does not exist (cache-repo-dir):
Cache directory does not exist (cache-files-dir):
Clearing cache (cache-dir): /home/<user>/.cache/composer
All caches cleared.
Generating autoload files
Generated autoload files
laravel
Cache directory does not exist (cache-vcs-dir):
Cache directory does not exist (cache-repo-dir):
Cache directory does not exist (cache-files-dir):
Clearing cache (cache-dir): /home/<user>/.cache/composer
All caches cleared.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   INFO  Discovering packages.

  cohensive/oembed ...................................................... DONE
  fruitcake/laravel-cors ................................................ DONE
  itsgoingd/clockwork ................................................... DONE
  jenssegers/agent ...................................................... DONE
  kalnoy/nestedset ...................................................... DONE
  laravel/fortify ....................................................... DONE
  laravel/jetstream ..................................................... DONE
  laravel/sail .......................................................... DONE
  laravel/sanctum ....................................................... DONE
  laravel/tinker ........................................................ DONE
  livewire/livewire ..................................................... DONE
  nesbot/carbon ......................................................... DONE
  nunomaduro/collision .................................................. DONE
  nunomaduro/termwind ................................................... DONE
  rappasoft/laravel-livewire-tables ..................................... DONE
  spatie/laravel-ignition ............................................... DONE
  spatie/laravel-permission ............................................. DONE
  theanik/laravel-more-command .......................................... DONE

> @php artisan vendor:publish --force --tag=livewire:assets --ansi

   INFO  Publishing [livewire:assets] assets.

  Copying directory [vendor/livewire/livewire/dist] to [public/vendor/livewire] ..................................... DONE

Generated optimized autoload files containing 6534 classes

The package gets symlinked under /vendor/open-graph as expected.

However, in /app/Services/TwitterService.php I have:

use Illuminate\Support\Facades\Log;
use OpenGraph\OpenGraph;
...

and when execution hits that use I get

Error: Class "OpenGraph\OpenGraph" not found

I have tried every variation of path and rebuilding autoload dependencies many, many times. I am (clearly) not very good with autoload and composer. Can anybody offer any suggestions?

0 likes
5 replies
jlrdw's avatar

Try without adding a autoload your self, let laravel auto discover it.

daveb2's avatar

@jlrdw Do you mean without use OpenGraph\OpenGraph ? I get Class "App\Services\OpenGraph" not found at the first line that tries to do a $obj = new OpenGraph();

jlrdw's avatar

@daveb2 no I mean

"autoload": {
        "psr-0": {
            "OpenGraph": "src/"
        }

Try to install without that part.

1 like
daveb2's avatar

@jlrdw Oh I see. I tried removing that from the open-graph package.json file and rebuilding (clear && cd local/open-graph/ && echo "open-graph" && composer clear-cache && composer dump-autoload && cd ../.. && echo "laravel" && composer clear-cache && composer dump-autoload) but I get the same error Class "OpenGraph\OpenGraph" not found when I try to do a $obj = new \OpenGraph\OpenGraph();

I have two local packages, and the other one works without issue and the package.json files are identical apart from the name of the package. The other package is TweetPHP, which is a fork of https://github.com/idealizetecnologia/tweet-php and does not depend on OpenGraph. In my main package.json I have:

	"repositories": [
        {
            "type": "path",
            "url": "./local/open-graph",
            "options": {
                "symlink": true
            }
        },
        {
            "type": "path",
            "url": "./local/tweet-php",
            "options": {
                "symlink": true
            }
        }
    ]

tweet-php works as expected, and a use TweetPHP\TweetPHP includes the /local/tweet-php/src/TweetPHP/TweetPHP.php file, but a use OpenGraph\OpenGraph does not include the /local/open-graph/src/OpenGraph/OpenGraph.php file, which is odd considering the config is almost identical.

daveb2's avatar

I eventually got this working... I had to remove the symlinks in /vendor and re-run composer update in each of the sub-repositories and then in the main top-level project.

Please or to participate in this conversation.