简体   繁体   English

为什么在运行“ composer install”命令时Composer找不到工匠?

[英]Why is Composer unable to locate artisan when running the “composer install” command?

I am using windows 7 and trying to install laravel 4.2 using composer. 我正在使用Windows 7,并尝试使用composer安装laravel 4.2。 My development environment is EasyPHP, but based on the research I have done, that is not the problem. 我的开发环境是EasyPHP,但是根据我所做的研究,这不是问题。

Every time I try to create a new laravel project, called "testapp", by running this command in my projects directory: 每次我尝试通过在我的项目目录中运行以下命令来创建一个名为“ testapp”的新laravel项目时:

composer create-project laravel/laravel testapp --prefer-dist

.., the installation is successful up until the point where composer tries to generate the "autoload" files, outputting these lines: ..,安装成功,直到composer尝试生成“自动加载”文件并输出以下行为止:

Generating autoload files
Could not open input file: artisan
Script php artisan clear-compiled handling the post-install-cmd event returned with an error

[RuntimeException]
Error Output:

If I then navigate to the new folder called testapp created in my projects directory, and run 如果然后我导航到在我的项目目录中创建的名为testapp的新文件夹,并运行

composer update

or 要么

composer install

I get the same error at the end. 最后我得到了同样的错误。

Although composer doesn't seem to be able to run any php artisan command, by navigating to my testapp folder and running: 尽管composer似乎无法运行任何php artisan命令,但请导航至我的testapp文件夹并运行:

php artisan {any command here}

I can accomplish any artisan command I want. 我可以完成我想要的任何工匠命令。 After doing some research, I found that the problem is with composer itself. 经过研究后,我发现问题出在作曲家本身。 Composer does not know how to find the artisan file. 作曲家不知道如何找到工匠文件。 One suggestion was that I edit the composer.json file so that artisan would be defined by an absolute path rather than a relative path. 一个建议是,我编辑composer.json文件,以使工匠将通过绝对路径而不是相对路径来定义。 So I changed this: 所以我改变了这个:

"scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ]
},

to this: 对此:

"scripts": {
        "post-install-cmd": [
            "php C:\\Program Files (x86)\\EasyPHP-DevServer-14.1VC11\\data\\localweb\\projects\\testapp\\artisan clear-compiled",
            "php C:\\Program Files (x86)\\EasyPHP-DevServer-14.1VC11\\data\\localweb\\projects\\testapp\\artisan optimize"
        ],
        "post-update-cmd": [
            "php C:\\Program Files (x86)\\EasyPHP-DevServer-14.1VC11\\data\\localweb\\projects\\testapp\\artisan clear-compiled",
            "php C:\\Program Files (x86)\\EasyPHP-DevServer-14.1VC11\\data\\localweb\\projects\\testapp\\artisan optimize"
        ],
        "post-create-project-cmd": [
            "php C:\\Program Files (x86)\\EasyPHP-DevServer-14.1VC11\\data\\localweb\\projects\\testapp\\artisan key:generate"
        ]
},

within the composer.json file in my testapp folder. 在我的testapp文件夹中的composer.json文件中。 Now, when running 现在,当跑步

composer install

I got this output in the command prompt: 我在命令提示符下得到了以下输出:

Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Warning: The lock file is not up to date with the latest changes in composer.json.
You may be getting outdated dependencies. Run update to update them.
Nothing to install or update
Generating autoload files
Script php C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\proje
cts\testapp\artisan clear-compiled handling the post-install-cmd event returned
with an error



[RuntimeException]
Error Output:



install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-p
lugins] [--no-custom-installers] [--no-scripts] [--no-progress] [-v|vv|vvv|--ver
bose] [-o|--optimize-autoloader] [packages1] ... [packagesN]

Although I first thought that this output meant that changing the composer.json would not solve the problem, I was amazed to find that changing the composer.json worked when the absolute path contained no spaces . 尽管我最初认为此输出意味着更改composer.json不能解决问题,但令我惊讶的是,当绝对路径不包含空格时,更改composer.json可以正常工作。

For example. 例如。 I went to C:\\Users\\AlexLeung\\Desktop, ran 我去了C:\\ Users \\ AlexLeung \\ Desktop,跑了

composer create-project laravel/laravel desktestapp --prefer-dist

got the initial error, then updated the composer.json in the new desktestapp folder to look like 出现初始错误,然后将新desktestapp文件夹中的composer.json更新为

"scripts": {
        "post-install-cmd": [
            "php C:\\Users\\AlexLeung\\Desktop\\desktestapp\\artisan clear-compiled",
            "php C:\\Users\\AlexLeung\\Desktop\\desktestapp\\artisan optimize"
        ],
        "post-update-cmd": [
            "php C:\\Users\\AlexLeung\\Desktop\\desktestapp\\artisan clear-compiled",
            "php C:\\Users\\AlexLeung\\Desktop\\desktestapp\\artisan optimize"
        ],
        "post-create-project-cmd": [
            "php C:\\Users\\AlexLeung\\Desktop\\desktestapp\\artisan key:generate"
        ]
},

, and running 和运行

composer install

was then successful. 然后成功了。 Although this shows I can install laravel if I just install it on a path with no spaces, I need to be able to install it in places under the "Program Files (x86)" directory since that is where EasyPHP, and its Apache web server root, are located. 尽管这表明如果只在没有空格的路径上安装laravel,就可以安装laravel,但我需要能够将其安装在“ Program Files(x86)”目录下的位置,因为这是EasyPHP及其Apache Web服务器的位置根,位于。 In trying to get the absolute path to work with spaces, I have tried using underscores, which ends up pointing to the wrong path, and \ , which generates the same error. 为了获得使用空格的绝对路径,我尝试使用下划线和\\ u0020,下划线最终指向错误的路径,\\ u0020生成相同的错误。

So the question remains: 因此问题仍然存在:

How can I get composer to recognize the artisan file within the newly created testapp folder? 如何让作曲家识别新创建的testapp文件夹中的artisan文件?

A set of resulting questions if the first can't be answered: 如果无法解决第一个问题,则会产生一系列问题:
1. Is there a way to get composer to recognize the artisan file without me needing to change the composer.json to iunclude an absolute path for artisan? 1.有没有一种方法可以使作曲家识别工匠文件,而无需更改composer.json来为工匠提供绝对路径?
2. Should I even be concerned about having the autoload files generated? 2.我什至应该担心生成自动加载文件吗? (is it even necessary) (甚至有必要)
3. Would it be okay if I just moved my projects to the desktop (or any other path without spaces) anytime I needed to run composer update or install? 3.是否可以在需要运行作曲家更新或安装的任何时候将项目移至桌面(或其他无空格的路径)的情况下使用?

Overall I would like to have a complete installation of laravel and be able to use the convenient composer commands whenever I need to. 总体而言,我希望完整安装laravel,并在需要时可以使用便捷的composer命令。

Maybe your issue has to do with some kind of permissions because you are trying to run commands inside "C:\\Program Files (x86)" . 也许您的问题与某种权限有关,因为您试图在“ C:\\ Program Files(x86)”中运行命令。 Try to deactivate UAC and see if that works. 尝试停用UAC,看看是否可行。

Also try to run php artisan clear-compiled from the command line. 也可以尝试从命令行中php artisan clear-compiled It may not even be an issue with composer but with the PHP path. 作曲家甚至可能都不是问题,但PHP路径可能不是问题。 Verify that your Windows PATH variable knows where your php.exe is located. 验证Windows PATH变量是否知道php.exe的位置。

I've just tested a clean install in a folder called "C:\\MyPrograms\\Some kind of folder\\" and composer runs as expected, so it's may have nothing to do with spaces. 我刚刚在名为“ C:\\ MyPrograms \\ Some kind of folder \\”的文件夹中测试了全新安装,并且composer可以按预期运行,因此它可能与空格无关。

You can get composer to recognize php artisan, one way or another. 您可以让作曲家以某种方式识别php artisan。 My personal experience tells me that having developer tools installed in C:\\Program Files is not a good idea because you will keep getting unexpected behaviors. 我的亲身经历告诉我,在C:\\ Program Files中安装开发人员工具不是一个好主意,因为您将不断收到意外的行为。 I advice you to instal PHP, Apache, EasyPHP & whatever you need in a new folder (like "C:\\MyPrograms" ). 我建议您将PHP,Apache,EasyPHP以及任何您需要的安装在新文件夹中(例如“ C:\\ MyPrograms” )。

Anyhow, even if you have PHP and Apache installed in the default program files folder, you can always set your Document Root of Apache to another location and this may also solve your issue. 无论如何,即使您在默认程序文件文件夹中安装了PHP和Apache,也可以始终将Apache的文档根目录设置到其他位置,这也可以解决您的问题。

As for your other questions: 至于您的其他问题:

2) Yes, it really is necessary. 2)是的,确实有必要。 The autoload files (will be located in vendor/composer/ ) enable you to work with namespaces and to work with your own classes and never to include() or require() because it will be done by Laravel automatically. 自动加载文件(将位于vendor/composer/ )使您能够使用名称空间并使用自己的类,而不再需要include()require()因为它将由Laravel自动完成。

3) I guess you could do that and it will work. 3)我想您可以做到,它将成功。 But the hassle of moving your entire project every time you need a composer update seems to much trouble and wasted time. 但是每次需要作曲家更新时都需要移动整个项目的麻烦似乎很麻烦,而且浪费时间。

I think you found a bug in Composer. 我认为您在Composer中发现了一个错误。

This line probably is supposed to make the commands used in the hooks executable: 该行可能应该使挂钩中使用的命令可执行:

https://github.com/composer/composer/blob/a8adbfeb9fc7861deade782938222714168a22a8/src/Composer/Command/RunScriptCommand.php#L89 https://github.com/composer/composer/blob/a8adbfeb9fc7861deade782938222714168a22a8/src/Composer/Command/RunScriptCommand.php#L89

I suspect that the result of realpath() with the binary directory will return the correct path, but including spaces. 我怀疑带有二进制目录的realpath()的结果将返回正确的路径,但包含空格。 putenv() looks like operating directly on the shell, with it's parameter being "PATH=C:\\Path with spaces\\somewhere;C:\\PreviousPaths\\" - this might not be working on Windows. putenv()看起来像直接在外壳上操作,其参数为"PATH=C:\\Path with spaces\\somewhere;C:\\PreviousPaths\\" -这可能在Windows上不起作用。

You should add an issue on Github to get this fixed. 您应该在Github上添加一个问题以解决此问题。

From experience, lots of software is not well prepared to deal with spaces in paths. 根据经验,很多软件没有做好充分的准备来处理路径中的空间。 I recently had to rename all my Jenkins job names because "job name === workspace directory name", and the backup module was acting up on the spaces. 最近,我不得不重命名所有Jenkins作业名称,因为“作业名称===工作区目录名称”,并且备份模块在这些空间上起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM