Composer update fails on own package - laravel

I've made a Composer package called nickdekruijk/larapages. It works fine when I do a composer required nickdekruijk/larapages for the first time. But after I add the service provider to config/app.php I can't run composer update anymore:
$ composer update
> php artisan clear-compiled
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
> php artisan optimize
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'NickDeKruijk\LaraPages\LaraPagesServiceProvider' not found
The only workaround so far is to remove the service provider (or comment the line) from config/app.php, run composer update, then put it back (uncomment) again which is pretty annoying. Am I doing something wrong? Do I need to fix my package maybe?

If the package is already installed, there shouldn't be an issue. Are you sure there isn't a problem with the namespacing of the package, and that the class actually exists?
As a workaround, one which is helpful for any related issue where a service provider isn't actually available yet, you can run Composer and not have it run Laravel's scripts (which cause the error).
composer update --no-scripts

I think I fixed it!
Problem was the autoloader.
First when I moved the files from src/* to src/NickDeKruijk/LaraPages/* composer update was working just fine. But I didn't want to move the files permanently (screwing up my git repo) so I added this to the package composer.json:
"autoload": {
"psr-4": {
"NickDeKruijk\\LaraPages\\": "src/"
}
}
Now I can run composer update just fine!

Related

fuelphp No composer autoloader found

I got error message when install fuelphp
No composer autoloader found. Please run composer to install the FuelPHP framework dependencies first!
Also i my composer is updated.
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
I am using PHP7 with a Vagrant environment.
Any idea about this error ?
It is not able to find installation of fuelphp. Most common issue is that index.php("public" or "public_html" folder) does not have the fuelphp's app, core and packages path set properly.
change DIR.'../ to DIR.'/../../(goes back one folder level)
There should be three of them.

Class 'Cmgmyr\Messenger\MessengerServiceProvider::class' not found

In my laravel project, I need messaging system. I trying to install "cmgmyr/laravel-messenger" and getting this error
"Class 'Cmgmyr\Messenger\MessengerServiceProvider::class' not found"
when i execute this command
php artisan vendor:publish --provider="Cmgmyr\Messenger\MessengerServiceProvider"
any suggestion to resolve this problem.
Most likely is that you have not installed the required dependency. Once you have added the dependency to your composer.json remember to run composer install.
To make this process really easy for yourself instead of adding the dependency to composer.json you can run composer require cmgmyr/laravel-messenger. This will add the latest version to your composer file and install the dependency for you.

forcing composer to regenerate autoloads when composer.json of a dependency is changed?

My workflow for developing Symfony bundles is the following:
install Symfony
create a git repo for the new bundle, put a composer.json file in there
require the new package in the top-level composer.json, using #dev version
composer update newpackage => the package is downloaded, using git clone
work on the git clone inside vendors, committing and pushing from it
This is all fine and dandy, but seems to break in one specific case:
if I alter the 'autoload' tag of the already-installed package, it seems that Composer has a hard time taking it into account:
I tried 'composer dumpautoload', and it does nothing
I do not want to remove the composer.lock file, as I do not want other packages to be updated to a newer version, I only want to alter the autoload config of that one package
I tried removing by hand vendor/composer/installed.json, and what happened is that Composer re-downloaded all the vendors and wiped any data which happened to be in there at that moment
The same problem manifested itself when I did alter the autoload section of the package on a separate clone, pushed the changes to git and ran 'composer update mypackage' - although that might have been related to packagist not having received the ping from github.
I can of course alter by hand the composer.lock and vendor/composer/installed.json files, but that seems too hackish. It also does not gives the guarantee that user downloading the package the 1st time will see it working.
Try:
./composer.phar dumpautoload -o
It reads the composer.json files and rewrites all the autoload files which pick up the new paths.
dumpautoload uses the package information from vendor/composer/installed.json and not the individual composer.json files. You need to change the autoload info there, too.
The file installed.json is only being update when you run a
composer update

How can I remove a package from Laravel using PHP Composer?

What is the correct way to remove a package from Laravel using PHP Composer?
So far I've tried:
Remove declaration from file composer.json (in the "require" section)
Remove any class aliases from file app.php
Remove any references to the package from my code :-)
Run composer update
Run composer dump-autoload
None of these options are working! What am I missing?
Composer 1.x and 2.x
Running the following command will remove the package from vendor (or wherever you install packages), composer.json and composer.lock. Change vendor/package appropriately.
composer remove vendor/package
Obviously you'll need to remove references to that package within your app.
I'm currently running the following version of Composer:
Composer version 1.0-dev (7b13507dd4d3b93578af7d83fbf8be0ca686f4b5) 2014-12-11 21:52:29
Documentation
https://getcomposer.org/doc/03-cli.md#remove
Updates
26/10/2020 - Updated answer to assert command works for v1.x and v2.x of Composer
I got it working... The steps to remove a package from Laravel are:
Remove the declaration from file composer.json (in the "require" section)
**Remove Service Provider from file config/app.php (reference in the "providers" array)
Remove any class aliases from file config/app.php
Remove any references to the package from your code :-)
Run composer update vendor/package-name. This will remove the package folder from the vendor folder and will rebuild the Composer autoloading map.
Manually delete the published files (read the comment by zwacky)
It will remove the package folder from the Vendor folder.
Normally composer remove used like this is enough:
composer remove vendor/package
But if a Composer package is removed and the "config" cache is not cleaned you cannot clean it. When you try like so
php artisan config:clear
you can get an error In ProviderRepository.php line 208:
Class 'Laracasts\Flash\FlashServiceProvider' not found
This is a dead end, unless you go deleting files:
rm bootstrap/cache/config.php
And this is Laravel 5.6 I'm talking about, not some kind of very old stuff.
It happens usually on automated deployment, when you copy files of a new release on top of old cache. Even if you cleared the cache before copying. You end up with an old cache and a new composer.json file.
You can remove any package just by typing the following command in the terminal, and just remove the providers and alias you provided at the time of installing the package, if any and update the composer,
composer remove vendor/your_package_name
composer update
Before removing a package from a composer.json declaration, please remove the cache:
php artisan cache:clear
php artisan config:clear
If you forget to remove the cache and you get a "class not found error" then please reinstall the package, clear the cache and remove again.
You can do any one of the below two methods:
Running the below command (most recommended way to remove your package without updating your other packages)
$ composer remove vendor/package
Go to your composer.json file and then run command like below it will remove your package (but it will also update your other packages)
$ composer update
If you are still getting the error after you are done with all the steps in the previous answers, go to your projects, Bootstrap → Cache → config.php. Remove the provider and aliases entries from the cached array manually.
Use:
composer remove vendor/package
This is an example:
Install or add a package
composer require firebear/importexportfree
Uninstall / remove
composer remove firebear/importexportfree
Finally after removing:
php -f bin/magento setup:upgrade
php bin/magento setup:static-content:deploy –f
php bin/magento indexer:reindex
php -f bin/magento cache:clean
Running
Composer remove package/name
Php artisan optimize
e.g "Composer remove mckenziearts/laravel-notify" works for me while using Laravel 8.
To add the packages, the command is to be like:
composer require spatie/laravel-permission
To remove the packages, the command is to be like:
composer remove spatie/laravel-permission
In case the given answers still don't help you remove that, try this:
Manually delete the line in require from composer.json
Run composer update
If this doesn't work "composer remove package/name", you can still remove it manually.
Note : package/name is like spatie etc.
Go to composer.json and find the package name
Delete package name from composer.json
Find the vendor file in your Laravel project.
Delete the package file which is under vendor
run composer install on your terminal
Note : Package File mean is that package that you are looking for. For example, you want to remove Spatie. Then you need to find that with similar name in vendor file and you need to delete it manually.
Your package was removed successfully.
We have come with a great solution. This solution is practically done in Laravel 6. If you want to remove any package from your Laravel Project then you can easily remove the package by following below steps:
Step 1: You must know the package name which you want to remove. If you don't know the complete package name then you can open your project folder and go to the composer.json file and check the name in the require array:
"require": {
"php": "^7.2",
"fideloper/proxy": "^4.0",
"laravel/framework": "^6.2",
"laravel/passport": "^8.3",
"laravel/tinker": "^2.0"
},
Suppose, here I am going to remove the "fideloper/proxy" package.
Step 2: Open a command prompt with your project root folder directory
Step 3: First of all, clear all cache by the following commands. Run the commands one by one.
php artisan cache:clear
php artisan config:clear
Step 4: Now write the following command to remove the package. Here you need to change your package name instead of my example package.
composer remove fideloper/proxy
Now, wait for a few seconds while your package is removed.
On Laravel 8.*, the following steps work for me:
Run command composer remove package-name on the terminal
Remove Provider and aliases from file Config/app.php
Remove the related file from the Config folder.
Remove it from your code where you used it.
Remove the package folder from the vendor folder (manual delete)
Remove it from file composer.json and 'composer.lock' files (use Ctrl + F5 to search)
Remove it from file config/app.php and file bootstrap/cache/config.php files
Run these commands:
composer remove **your-package-name**
php artisan cache:clear
php artisan config:clear
There are quite a few steps here:
Go to file composer.json and look for the package and how it is written over there.
for example
{ "require": {
"twig/twig": "^3.0" } }
I wish to remove twig 3.0
Now open cmd and run composer remove vendor/your_package_name as composer remove twig/twig will remove the package.
As a final step, run composer update. This will surely give you a massage of nothing to install or update, but this is important in case your packages have inter-dependencies.
You have two solution.
First
Use remove of composer.
composer remove *your_package_name*
Second
Delete the line in require from composer.json and then run update
composer update
After removed, recommend to run below two command.
php artisan cache:clear
php artisan config:clear

Do not update a specific package

Is there a way to tell composer that each time I do a composer update I want him to ignore a specific package?
Have you considered specifying the required version for the package you are trying to ignore? For instance:
"require": {
"some/package": "~1.2"
}
This may get updated, because you are saying any version >=1.2,<2.0, But if you strictly say you want only version 1.0, you should not see any updates to that package:
"require": {
"some/package": "1.2"
}
Actually I don't know if there is any way to tell composer to exclude one specific package from updating but you can tell which packages to update as
composer update <package> <package2>; // or
php composer.phar update <package> <package2>;
For example,
composer update foo/package1 bar/package2; // or
php composer.phar update foo/package1 bar/package2;
Also, I think, if you don't list them in composer.json (remove after installation) by yourself, then they will not be updated unless also specified in the list.
From Composer:
If you only want to install or update one dependency, you can whitelist them:
$ php composer.phar update monolog/monolog [...]
Check this link and also check Composer.
Update : (found on internet but not tested)
To do that, just remove the package from composer.lock
Update: Only availble for composer versions 1.0.0-alpha6 and lower. Using it in version 1.0.0-alpha7 and higher will remove all packages in "require-dev".
I believe currently you can trick composer with some mess if you can afford it in your project. Something like: Put all packages you don't want to update in "require-dev" and run updates with composer update --no-dev
Just be careful of that if you run composer install as i recall they will be removed from your project.
All this trickery is really nasty, so we should wait for official way of doing things like that, personally i update packages explicitly specifying them
To ignore a specific package, you can use provide (if it's part of your own package) or replace. This tells Composer that you wish to provide/replace a specific package, so it won't download it.
Here is the composer.json file example which should work:
{
"require": {
"radic/tmp-underscore-php": "~1.2.0"
},
"replace": {
"patchwork/utf8": "*"
}
}
In this example, the patchwork/utf8 package would be ignored on composer install or update.
To exclude specific version, see: Composer exclude specific versions.

Resources