Laravel-Artisan输入了错误的基本网址 [英] Laravel - Artisan gives wrong base url

查看:76
本文介绍了Laravel-Artisan输入了错误的基本网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的app/config/app.php具有

My app/config/app.php has

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

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

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::to具有相同的行为.

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

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

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

Any ideas why this happens ? Thanks!

推荐答案

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

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

  • 方案:http://https://
  • 主机:dev.domain.comlocalhost
  • 基础:something/somethingElse(Web服务器上的子目录)
  • 尾巴:myRoute,(Laravel路线参数)
  • 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)

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

These parts are then concatenated to form the URL.

最终,Laravel使用$_SERVER请求变量生成URL. Symfony\Component\HttpFoundation\Request中的功能prepareBaseUrl()最终用于确定URL的 base 部分.

Ultimately, Laravel generates the URL using the $_SERVER request variables. The function prepareBaseUrl() in Symfony\Component\HttpFoundation\Request is what is ultimately used to determine the base part of the URL.

当您通过网络浏览器发出请求时,请求将转到~/public/index.php,并使用正确的信息填充必要的$_SERVER变量,以使Laravel可以填充URL的 base 部分

When you make a request through your web browser, the request goes to ~/public/index.php and the necessary $_SERVER variables are populated with the correct information for Laravel to populate the base part of the URL.

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

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

从我发现的情况来看,Laravel团队似乎并没有胃口让应用程序在子目录(例如,子目录)中开箱即用地运行. Bugfix:子文件夹的域路由.

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.

我最终以@medowlock所描述的方式解决了我的问题,该脚本可以从命令行调用,例如:

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.:

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

这将在app/config/app.php相对 URL路由中设置的应用程序URL串联在一起.

This concatenates the application URL set in the app/config/app.php and the relative URL route.

这篇关于Laravel-Artisan输入了错误的基本网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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