【问题标题】:Laravel 5 url() doesn't contain schemeLaravel 5 url() 不包含方案
【发布时间】:2019-12-12 23:55:23
【问题描述】:

我是 Laravel 的新手。

我正在开发一个现有的 Laravel 5.8 项目。我使用 HomeStead 在本地安装它。

我注意到一个奇怪的重定向行为:考虑到我的主页是http://project.test/,当有重定向时,比如/redirected,Laravel 将位置设置为http://project.test/://project.test/redirected!当我试图弄清楚发生了什么事情时,我看到 url('/') Laravel 函数给了我 ://project.test 而不是 http://project.test

request()->getSchemeAndHttpHost() 给出http://project.test

web.php 中的示例:

Route::get('/info', function (Request $request) {


    echo url('/'); // gives '://project.test'
    echo '<br>';
    echo request()->getSchemeAndHttpHost(); // gives 'http://project.test'

    // This will lead me to 'http://project.test/://project.test/redirect'
    // return redirect('/redirect');
});


我对静态资源也有同样的问题。

配置:

  • 'url' =&gt; env('APP_URL', 'http://project.test')config/app.php
  • APP_URL=http://project.test.env 文件中。

我当然错过了一些东西,但到目前为止我找不到任何关于此的信息。

【问题讨论】:

  • config/app.php 文件中的“url”是什么?
  • 'url' =&gt; env('APP_URL', 'http://project.test')。和APP_URL=http://project.test.env 文件中。
  • 你的配置看起来不错。你能分享一下你使用 url() 的 sn-p 代码吗?
  • APP_URL 仅由 CLI 使用,当您通过网络服务器运行时它什么也不做
  • 我在描述中添加了一个例子。

标签: laravel laravel-5


【解决方案1】:

当你重定向时检查你是否写了:“Redirect::home()”,尝试“return Redirect::home()”(注意返回)

我测试了你的路线,对我来说效果很好。 我给你我的配置,只需将我的项目名称“tlara”替换为你的:“project”

在 host.sam 中

127.0.0.1 tlara.test #laragon 魔法! (注意不是我的ip 192.xx.xx,127.0.0.1是通用localhost)

在.env中

APP_URL=http://localhost
..
REDIS_HOST=127.0.0.1 (我不知道它是否相关) ..
SESSION_DOMAIN='本地主机'

清除

清除缓存(路由、缓存、视图)(可能没用)

测试

tlara.test/info
127.0.0.1/信息
本地主机/特拉拉/公共/信息

【讨论】:

  • 我不确定我是否理解答案,我将return redirect('/redirect'); 替换为return Redirect::home(),我得到了“InvalidArgumentException - Route [home] not defined。”。当我尝试使用Redirect::to('')Redirect::to('/') 时,我遇到了与以前相同的问题(重定向到http://project.test/://project.test)。
猜你喜欢
  • 2011-07-30
  • 1970-01-01
  • 1970-01-01
  • 2012-12-31
  • 2012-02-26
  • 2015-05-10
  • 2015-02-27
  • 2021-10-14
相关资源
最近更新 更多