I want to edit a package I pulled from composer in my Laravel 5 project, however i believe that if I ran composer update
and a new version of this package has been released, I will lose all of my changes. How should I go about editing the package? Is there a way to copy package out of the vendor directory so I can use it somewhere else in my project?

- 9,775
- 7
- 56
- 69

- 2,016
- 5
- 21
- 35
5 Answers
The simple, fast and safe method:
- Create a directory in the Laravel's root directory and name it packages or whatever you like.
- Move the modified package from vendor directory to your packages directory.
- Update composer.json to load the package from your packages directory instead of vendor directory.
first remove it from require
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.3.*",
"laravelcollective/html": "^5.3.0", <==== remove this line
"barryvdh/laravel-debugbar": "^2.3",
"doctrine/dbal": "^2.5"
},
and then add it to autoload
"autoload": {
"psr-4": {
"App\\": "app/",
"Collective\\Html\\": "packages/laravelcollective/html/src", <==== add this line
},
}
Please do not forget to run
composer dumpauto
Alternative for step 3.
There is also a new alternative if you're using latest version of composer.
Add this to you composer.json
"repositories": [
{
"type": "path",
"url": "./packages/laravelcollective"
}
]
And then modify the version of package to dev-master
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.3.*",
"laravelcollective/html": "dev-master", <==== this line
"barryvdh/laravel-debugbar": "^2.3",
"doctrine/dbal": "^2.5"
},
Finally
composer update

- 2,763
- 2
- 24
- 27
-
1When you're addressing the package e.g., Collective\\Html\\ make sure you check the package's `composer.json` and match your address with it. There might be more than one namespace that has to be addressed. – sinaza May 08 '20 at 15:06
-
Also take not that you are not using the composer file of the package. There might be auto loading for laravel in the package like this: `code "extra": { "laravel": { "providers": [ "Company\\Package" ] } } ` Make sure to add the package in /config/app.php => 'providers' array – Sjaak Wish Jun 09 '20 at 11:06
-
I love step 3 so much ^^ – Tien Hoang Nov 28 '22 at 14:04
It actually isn't safe to edit composer packages, for the very reason you point out.
What I do is extends the classes that I want/need to change.
I have done it here with the Filesystem class. It doesn't ensure that it won't break, but it does let you update without overwriting your changes.
config/app.php
<?php
return [
'providers' => [
// 'Illuminate\Filesystem\FilesystemServiceProvider',
'MyApp\Filesystem\FilesystemServiceProvider',
],
'aliases' => [
...
],
];
MyApp\Filesystem\FilesystemServiceProvider.php
<?php namespace MyApp\Filesystem;
use Config;
use Storage;
use League\Flysystem\Filesystem;
use Dropbox\Client as DropboxClient;
use League\Flysystem\Dropbox\DropboxAdapter;
use Illuminate\Filesystem\FilesystemManager as LaravelFilesystemManager;
class FilesystemManager extends LaravelFilesystemManager{
public function createDropboxDriver(array $config)
{
$client = new DropboxClient($config['token'], $config['app']);
return $this->adapt(
new Filesystem(new DropboxAdapter($client))
);
}
}

- 6,982
- 6
- 44
- 78
-
Thanks for the answer. In this method would I have to change the namespace in each class to "MyApp" or would it be better to remove the the original package from my composer.json after I've copied it? – TimothyBuktu Jun 26 '15 at 19:53
-
It depends on how much of the package you need to alter. For what I did, I only needed to update 2 classes, so I just pulled those two classes into my namespace, and made the adjustments. I left the original classes intact, and refer to the updated classes in my app. If it is extensive, you could always fork the repo on github and make your updates there. That way when there is a change it would give you time to update your code. – whoacowboy Jun 26 '15 at 20:07
-
2I usually fork the repo and reference my forked repository in my composer.json file under repositories section. Downside being you won't get updates when said package gets updated. – Matthew Brown Jun 26 '15 at 21:06
-
-
if you want to keep your changes AND update the package from the original repo at the same time, you can fork this package and point composer to pull from your fork, not the original repo.
All you have to do is add your fork as a repository and update the version constraint to point to your custom branch. Your custom branch name must be prefixed with dev-.
update your composer.json
file as follows:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/MyGithub/html"
}
],
"require": {
"laravelcollective/html": "dev-bugfix"
}
}
Note that you don't change the require statement except to specify your bugfix branch. You still reference the upstream package (laravelcollective/html), not your personal fork (MyGithub/html).
also, note that dev-
is automatically added so branch name is bugfix
not dev-bugfix
. if you named your branch as dev-bugfix
you will require it as dev-dev-bugfix
.

- 327
- 5
- 13
If you want to make changes to a class of a package you have to
Create a class that extends the package class and make your changes
Create a service provider that extends the service provider of the class and change the
registerBinding
class to bind your extended classPut that new service provider in
providers
array inconfig\app
Disable package discovery for this package, you may list the package name in the extra section of your application's composer.json file:
"extra": { "laravel": { "dont-discover": [ "barryvdh/laravel-debugbar" ] } },

- 25,960
- 22
- 158
- 247
- use strict version of the vendor package, e.g. instead of "vendor/package": "~1.3", or "vendor/package": "^1.3.2", you may use "vendor/package": "1.3.2",
- copy the modified source file into public (if open source), or storage/app for private file (eg laravel)
- simply code this in Controller : File::copy('foldername/filename.php', '../vendor/namevendor/subfolderwhatever/filename.php');
- make this as deployment routine , shared to deployment colleagues
- have a coffee, have a life, done

- 53
- 4