获取未定义的索引:REQUEST_URI - 在 Laravel 中运行 Artisan 命令时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42352663/
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 15:22:44  来源:igfitidea点击:

Getting Undefined index: REQUEST_URI - When Run Artisan Commands in Laravel

phplaravelartisan

提问by Vinny

I keep getting the error below every time I try to run an artisan command on Laravel, I'm on the project directory.

每次尝试在 Laravel 上运行 artisan 命令时,我都会收到以下错误,我在项目目录中。

For example, I run this command:

例如,我运行这个命令:

php artisan make:migration create_stats_table

And I get this error:

我收到这个错误:

  [ErrorException]              
  Undefined index: REQUEST_URI

No matter what command I run, I get the same error, even php artisan --versionreturns this error. How can I solve this problem?

无论我运行什么命令,都会出现相同的错误,甚至php artisan --version返回此错误。我怎么解决这个问题?

回答by michail_w

Your code expects to have this index, but you're running PHP in CLI mode. REQUEST_URIvariable of $_SERVERsuperglobal is only available if you're reaching script by browser.

您的代码希望有这个索引,但您在 CLI 模式下运行 PHP。 REQUEST_URI的变量$_SERVER,如果你通过浏览器到达脚本超全局才可用。

回答by AdamJones

As mentioned already this variable doesn't exist if you run by the CLI. If you can't get around the issue and avoid using it a work around is to use isset()

如前所述,如果您通过 CLI 运行,则此变量不存在。如果您无法解决该问题并避免使用它,则解决方法是使用 isset()

if (isset($_SERVER['REQUEST_URI'])){ 
...
}