1

I am trying to change the path output of {{ url('images/image.png') }} and {{ asset('images/image.png') }} Based on my reading, it sounds like I just need to set APP_URL in my .env file and off we go. Well, unfortunately that is not the case. I have set APP_URL=http://localhost in my .env file. I also set 'url' => env('APP_URL', 'http://broken.af') in my config/app.php file. Additionally, I have also tried setting ASSET_URL in both locations with the same result. I have also run artisan cache:clear, artisan config:clear and artisan config:cache. I also tried restarting the server just to be extra sure. For some reason Laravel keeps using the servers configured hostname for these values.

Not a dupe, we are not talking about the route paths, we are talking about the url and asset function and APP_URL and ASSET_URL not doing anything.

Alex Barker
  • 4,316
  • 4
  • 28
  • 47
  • what you should be looking for is `ASSET_URL` not `APP_URL` – Udo E. Aug 01 '19 at 23:24
  • 1
    Where is ASSET_URL documented... – Alex Barker Aug 01 '19 at 23:28
  • You should be able to set it in your `env` file and also in `config/app.php` – Udo E. Aug 01 '19 at 23:32
  • The only doc I can find on it is under https://laravel.com/docs/5.8/helpers documentation. It doesn't seem to have an effect. – Alex Barker Aug 01 '19 at 23:36
  • Ok. I use 5.8. If you have all your assets in the public folder you shouldn't have problems with assets except you move them to some other location – Udo E. Aug 01 '19 at 23:39
  • I am having an issue because I need to host it behind a reverse proxy, I have no idea why this is such a PITA. – Alex Barker Aug 01 '19 at 23:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/197370/discussion-between-udo-e-and-alex-barker). – Udo E. Aug 01 '19 at 23:44
  • Value in your `.env` file and `config/app.php` must be same. From your question, they aren't. – Udo E. Aug 02 '19 at 00:05
  • Possible duplicate of [Laravel routes behind reverse proxy](https://stackoverflow.com/questions/29912997/laravel-routes-behind-reverse-proxy) – Rwd Aug 02 '19 at 00:16
  • I definitely don't want to set the routing path, that works fine.This is not a duplicate of that. – Alex Barker Aug 02 '19 at 00:30

4 Answers4

1

I am going to go ahead and post the answer to my question. If UrlGenerator::forceRootUrl is not used, it will inevitably fallback to Request::root() which is implemented as follows: return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/');

I am not sure what ASSET_URL or SITE_URL do, if anything. These env variables are definitely not used in the url or asset functions. This does not appear to have changed in later versions of Laravel.

The only way I was able to find to get these variables to work was to manually read them in RouteServiceProvider::boot() and then call forceRootUrl.

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

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

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

The trace:

  1. https://github.com/illuminate/routing/blob/master/UrlGenerator.php#L204
  2. https://github.com/illuminate/routing/blob/master/UrlGenerator.php#L492
  3. https://github.com/illuminate/routing/blob/master/UrlGenerator.php#L494
  4. https://github.com/illuminate/http/blob/master/Request.php#L89
Alex Barker
  • 4,316
  • 4
  • 28
  • 47
  • Thank you save my weekend. I believe this step is needed in case of a badly configured Laravel application when going in production: the developer forgot something to enable the APP_URL (I'm not a Laravel expert as you can see). – Kartoch Jul 10 '20 at 16:55
0

As you are trying to get an asset, in your case it's an image it will be right to use asset() laravel helper,

for more information, please check laravel method-asset helper

Thamer
  • 1,896
  • 2
  • 11
  • 20
0

If you have all your assets in the default public folder, use the Fully Qualified Domain Name (including security protocol) for your APP_URL in your .env file. Example:

APP_URL=https://broken.af

Also use the same for your config/app.php file:

'url' => env('APP_URL', 'https://broken.af'),
Udo E.
  • 2,665
  • 2
  • 21
  • 33
0

If this is helpful for someone, this code worked for me:

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

This is how I'm saving the file previously:

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

I found this answer here

p.d. Don't forget to do the symlink like documentation mention here

php artisan storage:link
mauronet
  • 850
  • 12
  • 17