如何在 Laravel 中正确安装包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34726101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:01:54  来源:igfitidea点击:

How to properly install package in Laravel?

phplaravellaravel-5laravel-5.1

提问by cyber8200

Recently, I installed the a package LaravelFacebookSdk.

最近,我安装了 a 包LaravelFacebookSdk



Install

安装

I update my composer.jsonby adding

composer.json通过添加更新我的

"sammyk/laravel-facebook-sdk": "~3.0"

Then, I run composer update

然后,我跑 composer update



Service Provider

服务提供者

In my /config/app.php, I add the LaravelFacebookSdkServiceProviderto the providers array.

在 my 中/config/app.php,我将 加入LaravelFacebookSdkServiceProvider到 providers 数组中。

'providers' => [
    SammyK\LaravelFacebookSdk\LaravelFacebookSdkServiceProvider::class,
    ];


Everything works great. Then, I pushed it to my repository.

一切都很好。然后,我将其推送到我的存储库。



Here comes the issue !

问题来了!

Second developer coming in did a git pulland run composer update

第二个开发人员进来做了一个git pull并运行composer update

He will get an error

他会得到一个错误

 SammyK\LaravelFacebookSdk\LaravelFacebookSdkServiceProvider::class,

is undefine. because, I declared that in my /config/app.phpunder my providers array.

未定义。因为,我/config/app.php在我的提供者数组下声明了这一点。

He have to go comment outthat line, and run the composer updatefirst. After everything successfully installed, then go back in and uncommentthat line back again.

他必须去注释掉那行,然后运行composer update第一行。一切都成功安装后,然后返回并再次取消注释该行。

Will other developer have to do this each time, we installed a new package ?

每次我们安装一个新软件包时,其他开发人员都必须这样做吗?

Am I missing something here ?

我在这里错过了什么吗?

Please kindly advise if I did anything wrong.

如果我做错了什么,请告知。

采纳答案by Bogdan

The problem here is that there is a php artisan clear-compiledcommand being configured to run before the update process in your composer.jsonfile. And since artisanis an integral part of the Laravel app, it will complain when there's something wrong with the app code. Since you have a reference to a class that is not yet present, it will spit out that RuntimeException. You can fix that by moving that command from the pre-update-cmdlist to post-update-cmdlist in your composer.json.

这里的问题是有一个php artisan clear-compiled命令被配置为在composer.json文件中的更新过程之前运行。由于它artisan是 Laravel 应用程序的一个组成部分,当应用程序代码有问题时它会抱怨。由于您引用了一个尚不存在的类,它会吐出那个RuntimeException. 您可以通过将该命令从pre-update-cmd列表移动到post-update-cmd您的composer.json.

So change this:

所以改变这个:

"scripts": {
    ...
    "pre-update-cmd": [
        "php artisan clear-compiled"
    ],
    "post-update-cmd": [
        "php artisan optimize"
    ]
},

To this:

对此:

"scripts": {
    ...
    "pre-update-cmd": [
    ],
    "post-update-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ]
},

Now the clear-compiledcommand will be run after the update process, when the referenced LaravelFacebookSdkServiceProviderclass is present, so no more errors.

现在该clear-compiled命令将在更新过程之后运行,当引用的LaravelFacebookSdkServiceProvider类存在时,不再有错误。

回答by jfadich

Instead of running composer updaterun composer install. There is no need to change the commands in your json file.

而不是运行composer updaterun composer install。无需更改 json 文件中的命令。

When you run composer updateit will go through all your packages and update to the most recent minor version based on your composer.json then update the composer.lock. This isn't what you want.

当你运行composer update它时,它会遍历你所有的包并根据你的 composer.json 更新到最新的次要版本,然后更新 composer.lock。这不是你想要的。

When you run composer installit will make sure everything in your json file is installed, including packages you just added. This is what your looking for.

当您运行时composer install,它将确保您的 json 文件中的所有内容都已安装,包括您刚刚添加的软件包。这就是你要找的。