chrisan's avatar

route() is returning localhost:8000 despite changing configs

edit: For anyone reading this and not going through the whole thread, this issue was showing when using https://expose.dev/ - it does not happen with https://ngrok.com/ Original post below:

I'm getting this on my local dev box when using php artisan serve. I feel like I'm missing something silly. I've tried php artisan route:clear and php artisan cache:clear

I've got the .env

APP_DOMAIN=asdf.sharedwithexpose.com
APP_URL=https://${APP_DOMAIN}

and app.php

  'url' => env('APP_URL', 'https://asdf.sharedwithexpose.com'),
  'domain' => env('APP_DOMAIN', 'asdf.sharedwithexpose.com'),

When I run php artisan tinker and dump route('login') I get the correct domain, but if I edit a test route in web.php the route()s both show localhost

  Route::get('foo', function() {
    dump(Route::getCurrentRoute()->middleware());  # only web loaded
    dump(env('APP_URL'));  # correct output
    dump(env('APP_DOMAIN')); # correct output
    dump(config('app.url')); # correct output
    dump(config('app.domain')); # correct output
    dump(route('login', [], true)); # incorrect localhost
    dump(route('home')); # incorrect localhost
  });
0 likes
8 replies
MohamedTammam's avatar

When you run php artisan serve it uses localhost not a custom domain.

chrisan's avatar

@MohamedTammam is there anyway to override this for use with? sharedwithexpose or ngrok?

This is required for Shopify App development which needs a https url that is loaded in an iframe on their site. Passing their domain to --host just results in artisan failing to get an open port

php artisan serve --host asdf.sharedwithexpose.com

WARN  Failed to listen on asdf.sharedwithexpose.com:8000 (reason: Cannot assign requested address).

WARN  Failed to listen on asdf.sharedwithexpose.com:8001 (reason: Cannot assign requested address).

WARN  Failed to listen on asdf.sharedwithexpose.com:8002 (reason: Cannot assign requested address).

WARN  Failed to listen on asdf.sharedwithexpose.com:8003 (reason: Cannot assign requested address).

WARN  Failed to listen on asdf.sharedwithexpose.com:8004 (reason: Cannot assign requested address).

WARN  Failed to listen on asdf.sharedwithexpose.com:8005 (reason: Cannot assign requested address).

WARN  Failed to listen on asdf.sharedwithexpose.com:8006 (reason: Cannot assign requested address).

WARN  Failed to listen on asdf.sharedwithexpose.com:8007 (reason: Cannot assign requested address).
MohamedTammam's avatar

@chrisan Run

php artisan serve

Then run ngrok

ngrok http http://localhost

PS: Make sure to add your port if it isn't the default.

chrisan's avatar

@MohamedTammam that is exactly what I'm doing in my first post which still outputs localhost for the routes instead of the ngrok (or sharedwithexpose) url

I guess since I only really need it for a few urls, I'll just do the app.url + the relative path route

Snapey's avatar

your site will always be localhost internally, but might be you expose route externally

The fact that login route shows localhost just means it is reflecting back the url the request came in on

app_url is only used when you need the sites route when not responding to a request, eg such as in jobs

chrisan's avatar

@Snapey

The fact that login route shows localhost just means it is reflecting back the url the request came in on

I am requesting it on https://asdf.sharedwithexpose.com/foo which communicate through my local process of expose which in turn hit http://localhost:8000 - so I expected it to use asdf.sharedwithexpose.com/login

I switched to them instead of ngrok b/c the whole issue I'm testing is about 3rd party cookies in Safari and since ngrok requires one to use - it was a non-starter.

However when I go back to ngrok it works as expected. Turns out expose doesn't populate $_SERVER['HTTP_HOST'] or $_SERVER['HTTP_X_FORWARDED_HOST'] like 'ngrok does, http_host is localhost and x_forwarded is not even set, there's the root cause. Doh.

So this whole issue is sharedwithexpose. I need to find another tunnel service - thanks for all the replies :)

rodrigo.pedra's avatar

APP_URL is only used on CLI processes, such as artisan and queue workers.

On HTTP processes, Laravel uses the request's domain.

If you are using php artisan serve that will be a localhost URL.

Take a look at Laravel Valet (there is a fork for Linux), or Laravel Herd. Those tools will allow you to use a custom local domain, and also manage local SSL.

Please or to participate in this conversation.