Laravel 5.5 APP_URL不变

时间:2019-08-01 23:11:49

标签: php laravel

我正在尝试根据自己的阅读更改Element type is invalid: expected a string (for built in components) or a class/function (for composite components) but got object.{{ url('images/image.png') }}的路径输出,听起来我只需要在.env文件中设置APP_URL即可。好吧,不幸的是事实并非如此。我在.env文件中设置了{{ asset('images/image.png') }}。我还在我的config / app.php文件中设置了APP_URL=http://localhost。另外,我还尝试了在两个位置设置ASSET_URL的结果相同。我还运行了'url' => env('APP_URL', 'http://broken.af')artisan cache:clearartisan config:clear。我还尝试重新启动服务器,只是为了更加确定。由于某些原因,Laravel会继续使用服务器配置的主机名获取这些值。

不是傻瓜,我们不是在谈论路由路径,我们是在谈论url和asset函数,APP_URL和ASSET_URL没有做任何事情。

4 个答案:

答案 0 :(得分:0)

在尝试获取资产时,就您的情况而言,它是一张图片,可以使用asset() laravel帮助器,

有关更多信息,请检查laravel method-asset helper

答案 1 :(得分:0)

如果所有资产都位于默认公用文件夹中,请为APP_URL文件中的.env使用完全限定域名(包括安全协议)。示例:

APP_URL=https://broken.af

还对您的config/app.php文件使用相同的内容:

'url' => env('APP_URL', 'https://broken.af'),

答案 2 :(得分:0)

我将继续发布问题的答案。如果不使用UrlGenerator::forceRootUrl,它将不可避免地回退到Request::root(),其实现方式如下:return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/');

我不确定ASSET_URL或SITE_URL做什么(如果有的话)。在urlasset函数中绝对不使用这些env变量。在更高版本的Laravel中似乎没有改变。

我能够找到使这些变量起作用的唯一方法是在RouteServiceProvider::boot()中手动读取它们,然后调用forceRootUrl。

    public function boot()
    {
        parent::boot();

        /** @var UrlGenerator $url */
        $url = $this->app['url'];

        // Force the application URL
        $url->forceRootUrl(config('app.url'));
    }

跟踪:

  1. C# Is it thread safe to subscribe Same event handler for all Objects
  2. https://github.com/illuminate/routing/blob/master/UrlGenerator.php#L204
  3. https://github.com/illuminate/routing/blob/master/UrlGenerator.php#L492
  4. https://github.com/illuminate/routing/blob/master/UrlGenerator.php#L494

答案 3 :(得分:0)

如果这对某人有帮助,则此代码对我有用:

$url = env('APP_URL').Storage::url($file->path);

这是我以前保存文件的方式:

$file->path = $request->file("file")->store("public");

我找到了这个答案here

p.d。别忘了做符号链接,例如文档提及here

php artisan storage:link