Laravel - Artisan 给出了错误的基本网址

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

Laravel - Artisan gives wrong base url

phplaravellaravel-4

提问by medowlock

My app/config/app.php has

我的 app/config/app.php 有

'url' => 'http://dev.domain.com/something/somethingElse'

Then I have a function that can be called from the application and also from an artisan command. But URL::route('myRoute')returns different results. When called from the application it returns http://dev.domain.com/something/somethingElse/myRoute, but in the artisan command http://dev.domain.com/myRoute.

然后我有一个可以从应用程序和工匠命令调用的函数。但URL::route('myRoute')返回不同的结果。当从应用程序调用时,它返回http://dev.domain.com/something/somethingElse/myRoute,但在 artisan 命令中http://dev.domain.com/myRoute

URL::tohas same behaviour.

URL::to有相同的行为。

Note: I do not have any other app.php file defined for other environments that could overwrite the global one.

注意:我没有为其他环境定义任何其他可能覆盖全局环境的 app.php 文件。

Any ideas why this happens ? Thanks!

任何想法为什么会发生这种情况?谢谢!

回答by Marchie

A Laravel-generated URL consists of a number of parts:

Laravel 生成的 URL 由许多部分组成:

  • Scheme: http://, https://, etc.
  • Host: dev.domain.com, localhost, etc.
  • Base: something/somethingElse(the subdirectory on the web server)
  • Tail: myRoute, (the Laravel route parameters)
  • 方案:http://https://,等。
  • 主持人:dev.domain.comlocalhost,等。
  • Base:something/somethingElse(Web服务器上的子目录)
  • 尾部:myRoute,(Laravel 路由参数)

These parts are then concatenated to form the URL.

然后将这些部分连接起来形成 URL。

Ultimately, Laravel generates the URL using the $_SERVERrequest variables. The function prepareBaseUrl()in Symfony\Component\HttpFoundation\Requestis what is ultimately used to determine the basepart of the URL.

最终,Laravel 使用$_SERVER请求变量生成 URL 。prepareBaseUrl()in函数Symfony\Component\HttpFoundation\Request最终用于确定URL的基本部分。

When you make a request through your web browser, the request goes to ~/public/index.phpand the necessary $_SERVERvariables are populated with the correct information for Laravel to populate the basepart of the URL.

当您通过 Web 浏览器发出请求时,请求会转到~/public/index.php$_SERVER使用正确的信息填充必要的变量,以便 Laravel 填充URL的基本部分。

However, when you make the request using Artisan on the command line, the request goes to the ~/artisanscript. This means that the $_SERVERvariables are not populated in the same way and Laravel is not able to determine the basepart of the URL; instead, it returns an empty string ''.

但是,当您在命令行上使用 Artisan 发出请求时,请求会转到~/artisan脚本。这意味着$_SERVER变量的填充方式不同,Laravel 无法确定URL的基本部分;相反,它返回一个空字符串''

From what I can find, it doesn't look like there is any appetite from the Laravel team to enable the application to function out-of-the-box in a subdirectory, e.g. Bugfix: domain routing for subfolder.

据我所知,Laravel 团队似乎没有任何兴趣让应用程序在子目录中开箱即用,例如Bugfix: domain routing for subfolder

I ended up working around it in the way described by @medowlock for my scripts that would be called from the command line, e.g.:

我最终以@medowlock 描述的方式解决了它,我的脚本将从命令行调用,例如:

Config::get('app.url') . URL::route('route.name', ['parameter'=>'value'], false)

Config::get('app.url') . URL::route('route.name', ['parameter'=>'value'], false)

This concatenates the application URL set in the app/config/app.phpand the relativeURL route.

这将连接在app/config/app.php相对URL 路由中设置的应用程序 URL 。

回答by Maxime Rainville

If you use Laravel 4.2, you can call the URL::forceRootUrlfunction to explicitly define what the root url of your laravel application is.

如果您使用 Laravel 4.2,您可以调用该URL::forceRootUrl函数来明确定义您的 Laravel 应用程序的根 url 是什么。

URL::forceRootUrl( Config::get('app.url') );

Calls to URL::towill use the URL define in your app config file after forcing the Root URL.

URL::to在强制使用根 URL 后,调用将使用应用配置文件中定义的 URL。

Unfortunately, this isn't available in Laravel 4.1 or Laravel 4.0

不幸的是,这在 Laravel 4.1 或 Laravel 4.0 中不可用