定义 Laravel 应用程序的基本 URL

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

Define base url for laravel application

phplaravellaravel-4

提问by Taig

My client's application is temporarily running on my staging server (my-domain.com), while on his server (client-domain.com) I placed an .htaccessfile to redirect all requests to my staging server. This works fine so far except when building URLs with Laravel's helper.

我的客户的应用程序暂时在我的登台服务器 ( my-domain.com)上运行,而在他的服务器 ( client-domain.com) 上,我放置了一个.htaccess文件以将所有请求重定向到我的登台服务器。到目前为止,这工作正常,除非使用 Laravel 的帮助程序构建 URL。

When I call {{ action( 'Controller@getIndex' }}for instance, Laravel returns my-domain.com/index(instead of the desired client-domain.com/index).

{{ action( 'Controller@getIndex' }}例如,当我打电话时,Laravel 返回my-domain.com/index(而不是所需的client-domain.com/index)。

For now I solved this by replacing these calls with absolute, hard coded urls but that is disturbing my workflow.

现在我通过用绝对的、硬编码的 url 替换这些调用来解决这个问题,但这扰乱了我的工作流程。

So, is there a way to force Laravel to use a specific base url?

那么,有没有办法强制 Laravel 使用特定的基本 url?

回答by Atrakeur

Laravel generate url for the current domain it is running on. Currently, laravel is running on my-domain.com even if you are calling it using client-domain.com. That depend on how you are redirecting of course.

Laravel 为其运行的当前域生成 url。目前,laravel 正在 my-domain.com 上运行,即使您使用 client-domain.com 调用它。当然,这取决于您如何重定向。

So, firstly why are you redirecting the client domain to your server.

那么,首先为什么要将客户端域重定向到您的服务器。

If it's a permanent redirection, then it's better for you to:

如果是永久重定向,那么您最好:

  • consider it just as it is: a redirection
  • consider changing your architecture to support both domains equally (no redirection so)
  • 照原样考虑:重定向
  • 考虑更改您的架构以平等地支持两个域(因此无需重定向)

If it's just for testing/debugging (or whatever) and this redirection will go away when you will move into production stage, you don't have to worry about that:

如果它只是用于测试/调试(或其他)并且当您进入生产阶段时此重定向将消失,您不必担心:

  • laravel is creating links to avoid doing the extra, useless redirection on each page load, in fact it's a good thing performance wise.
  • when you'll go into production an install the app directly in client-domain.com, laravel will make all links points to that location automaticly, so you'll be safe to remove your app on my-domain.com
  • laravel 正在创建链接以避免在每个页面加载时进行额外的、无用的重定向,事实上,这在性能方面是一件好事。
  • 当您进入生产环境并直接在 client-domain.com 中安装应用程序时,laravel 将使所有链接自动指向该位置,因此您可以安全地在 my-domain.com 上删除您的应用程序

On a side note, it's not really recommended to have the same content accessible with multiple urls. So, you have to 301 redirect one domain to the other (seems to be the current behavior) and then use only one of them.

附带说明一下,不建议使用多个 url 访问相同的内容。因此,您必须 301 将一个域重定向到另一个域(似乎是当前的行为),然后仅使用其中一个域。

Edit: and to answer you last question, there is no way to set the base url in laravel (for now at least). There seems to be an issue on github about that (here: https://github.com/laravel/framework/issues/92). Maybe you can investigate in that direction, but I don't think it'll be implemented one day because that seems to be hacky and quite uncommon in any regular web application.

编辑:并回答您的最后一个问题,无法在 laravel 中设置基本 url(至少现在是这样)。github 上似乎存在一个问题(此处:https: //github.com/laravel/framework/issues/92)。也许您可以朝那个方向进行调查,但我认为它不会有朝一日会实施,因为这在任何常规 Web 应用程序中似乎都很棘手且非常罕见。