在本地使用Composer,然后通过FTP上传文件 [英] Using Composer locally then uploading files through FTP

查看:271
本文介绍了在本地使用Composer,然后通过FTP上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些奇怪的设置,但是遇到了一个项目,在该项目中,在本地环境中使用Composer来启动项目。最初的开发人员没有ssh访问生产服务器的权限,因此他在本地使用Composer,并使用FTP将其 vendor目录从他的桌面上传到服务器上。

Bit of a strange set up but have come across a project where Composer has been used in a local environment to get a project started. The original developer did not have ssh access to the production server therefore he used Composer locally and uploaded the 'vendor' directory from his desktop to the server using FTP.

我现在需要添加 PHPMailer 程序包,因此必须在Mac上本地完成以下操作:

I now need to add the PHPMailer package so have done the following locally on my Mac:

cd Desktop/
composer require phpmailer/phpmailer

这在我的桌面上创建了以下结构:

This has created the following structure on my desktop:

Desktop/composer.json
Desktop/composer.lock
Desktop/vendor/autoload.php
Desktop/vendor/composer/*
Desktop/vendor/phpmailer/*

我需要通过FTP上传哪些?我意识到 vendor / phpmailer / * 是我想要的软件包,因此需要上载。

Which of these do I need to upload through FTP? I realise vendor/phpmailer/* is the package I want, so will need uploading.

其他的呢? ?我已经配置了自动装带器,所以这里不需要猜测 vendor / autoload.php 吗?

What about the others? I already have an autoloader configured so guessing vendor/autoload.php is not required here?

composer.json 我可以将包添加到已有的包中,例如

composer.json I could add the package to what's already there, e.g.

"require": {
    *other packages here*
    "phpmailer/phpmailer": "^5.2"
}

但是我不确定是否有必要,因为我不会在ssh / Composer上使用服务器运行任何更新?

But I wasn't sure if that's necessary because I'm not going to be using ssh/Composer on the server to run any updates?

推荐答案

通常的工作流程是:


  1. 从版本控件中检出当前版本。

  2. 通过命令行添加依赖项 composer需要新的/软件包

  3. 这将下载新软件包并更新自动加载功能。

  4. 在本地或在测试网站环境中测试结果。

  5. 如果对结果满意,请将整个文件夹上载到生产服务器。

  1. Checkout the current version from the version control.
  2. Add dependencies via command line composer require new/package.
  3. This will download the new package and update the autoloading.
  4. Test the result locally or on a test website environment.
  5. If happy with the result, upload the whole folder to the production server.

可能此一般工作流程的几个例外情况:

There may be several exceptions from this general workflow:

ad 1:如果没有版本控制l,您最好立即开始本地git repo,并在第一次提交时将当前的生产状态下载到本地。没有版本控制将使事情变得更困难,尤其是回到已知的工作版本。并且由于生产服务器上的文件可能不受管理,因此您还需要将 vendor 文件夹检入新创建的版本控件中,以避免取消已进行的任何更改。

ad 1: If there is no version control, you'd probably better of starting a local git repo right now, and download the current production state into it as the first commit. Not having version control will make things harder, especially going back to known working versions. And because the files on the production server are probably unmanaged, you'd also check in the vendor folder into your newly created version control just to avoid canceling any changes that had been made to these files.

ad 2:手动编辑 composer.json 文件有时是获取内容的较快方法如果您知道自己在做什么,则需要,但必须正确编辑JSON。对我来说,如果我已经准备好命令行,通常太麻烦了。该命令还将选择适合已安装依赖项的匹配版本。手动编辑可能会导致您不得不取消版本冲突。请记住,仅在生产环境中安装与PHP版本兼容的依赖项。您可能应该运行 composer config platform.php XYZ ,以便将PHP的生产版本添加到 composer.json 文件,该文件可防止Composer根据您的开发PHP安装依赖项版本。添加 -g 开关会将此设置添加到全局(用户)设置中,这将影响您启动的所有作曲家操作,也影响其他项目。

ad 2: Manually editing the composer.json file sometimes is a faster way to get what you want if you know what you are doing, but you'd have to correctly edit the JSON. For me it usually is too much hassle if I already have a command line ready. The command will also select a matching version that fits into the already installed dependencies. Manual editing may lead to version conflicts that you'd have to untangle. Remember to only install dependencies that work with the PHP version in production. You probably should run composer config platform.php X.Y.Z in order to add the production version of PHP into the composer.json file, which prevents Composer from installing dependency versions based on your development PHP. Adding the -g switch will add this setting to your global (user) setting instead, which will affect all composer operations you start, also for other projects.

ad 3:手动编辑将要求您在命令行上运行 composer update ,因此可能没有理由不执行 composer要求

ad 3: Manual editing will require you to run composer update on the command line, so there's probably no reason to not do composer require instead.

广告4:如何完成此操作完全取决于您所使用的环境。

ad 4: How this could be done is entirely dependent on what environment you have to work with.

广告5:在此阶段,您已经组装了创建工作网站所需的所有文件。除非上传因某种原因失败,否则将它们上传到生产环境将始终使网站正常运行。如果您担心FTP不可靠,也可以使用一些先上传到临时文件夹,然后在服务器上移动的方法。有些人采取不同的方法:他们在生产服务器上有一个git存储库,他们只是将应发布的版本推送到该远程仓库中。然后,某些后推脚本将运行 composer install 。这种自动方法也可以使用(但不能使用FTP),但是在部署过程中发生某些故障的风险更高,并且可能无法轻松地回到以前的情况。

ad 5: At this stage you have assembled all files necessary to create a working website. Uploading them to production will always result in a working website unless the upload fails somehow. You could also use some "upload first to temporary folder, then move on the server" approach if you fear FTP would be unreliable. Some people take a different approach: They have a git repository on the production server and they simply push the version that should go live onto that remote repo. Some post-push scripts will run composer install then. This automated approach will also work (but not using FTP), but has the higher risk of something failing during deployment, and probably has no easy way back to the previous situation.

最后,我要说的是通过FTP上传整个文件夹结构(嗯,该协议本身是不安全的,最好用FTPS(带有SSL的FTP)替换它) ,SFTP或SCP)比在生产服务器上运行Composer更好。

So in the end I'd say that uploading the whole folder structure via FTP (well, that protocol is insecure itself, better replace it with FTPS (FTP with SSL), SFTP or SCP) is better compared to running Composer on the production server.

您要上传的文件夹的具体问题:全部。尤其要上载整个 vendor 文件夹。它包含当前的自动加载器以及软件所需的所有依赖包。如果工作正常,请下载现有的 composer.json composer.lock 文件以及所有其他文件,然后添加对它的新依赖性。这改变了这两个文件,将新包添加到 vendor 文件夹中,并将类添加到自动加载器。

Your specific question regarding which folders to upload: All of them. Especially upload the whole vendor folder. It contains the current autoloader and all dependency packages the software needs. If you worked correctly, you downloaded the existing composer.json and composer.lock file together with everything else and added the new dependency to it. This has changed both these files, added the new package to the vendor folder and the classes to the autoloader.

不要只上传 vendor 文件夹的一部分,或者手动编辑自动加载的组件。如果您在某些方面做错了,您只会为开发人员带来惊喜,如果您做错了某些方面的事情,这也会花费更多时间。 Composer是管理依赖项的非常好的工具-使用它!

Don't fiddle with uploading only parts of the vendor folder, or manually editing a component of the autoloading. You will only create surprises for the developer coming after you if you do some aspect incorrectly, and it also takes more time. Composer is a very good tool to manage dependencies - use it!

这篇关于在本地使用Composer,然后通过FTP上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆