Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

packy's avatar
Level 7

Forge Command not using right URL

This is quite frustrating. I have a command that runs a notification that uses the URL function to have a link in an email. When I use a function in controller and hit a route to send the notification the URL returns the correct link, when the command is run in dev.mystite.com the correct URL is ran, when the command is ran on the production site it returns http://localhost

The part of the notication with the URL ->action('View WOD', url( 'leagues/' . $this->league->id) );

My ENV file on production server APP_URL=https:mysite.com/

My app config file 'url' => env('APP_URL'),

In my command (when this is ran it notification returns localhost):

$athlete->notify( new WodStarts( $league ) );

in API Controller to run manually to test (when this is ran it notification returns url from env file)

$athlete->notify( new WodStarts( $league ) );

What I have already checked:

  • I deleted the defualt site
  • I deleted the scheduler that ran on the default site
  • I checked both env files for the 2 sites (production is https:mysite.com/ and dev is https://dev.mysite.com.com/)
  • I cant find any reference to localhost
  • Notification DO use right URL from Dev site
  • Notification DO use right URL on production when ran from controller
  • Notifications DO NOT use right URL when ran from command in production

What is going on? Where is it finding localhost and why would a notification use the right url from a controller but not a command???

0 likes
21 replies
click's avatar
click
Best Answer
Level 35

Not sure if it is a typo or it actually is like that but your APP_URL has an invalid url it should be APP_URL=https://mysite.com(slashes are missing).

Did you try clearing the cache? php artisan cache:clear ?

And what does config('app.url'); output when you run it on production in the command?

packy's avatar
Level 7

@m-rk I just noticed that url is incorrect but like I said that notification and url works when I run the notification manually from a controller. It only returns localhost when the notification is ran from a command. I updated my question to show you the call in the command and the controller. Exact same but the command notification returns wrong url

packy's avatar
Level 7

@mk also note I run php artisan cache:clear on my deploy script so that is not the issue.

click's avatar

Yes but I am trying to help you debug here. What does config('app.url') return on your production if you run it as an artisan command?

Or maybe use tinker:

php artisan tinker
config('app.url');
packy's avatar
Level 7

@m-rk the only place in the code at all where there is localhost is in the .env.example file and in 'host' => 'localhost', in beanstalk. I dont think either should apply? I am just more baffled why it works in the dev site and from a controller but not a command since they all use the same notification

click's avatar

Hmm ok. And you do not mention it in your question but running the same command (artisan) from dev is working ok, right?

At first I was thinking the difference between your controller and your artisan command is that your controller does a real http request and has a Request object where it gets the domain from. But that is probably not the issue here because if I just run url('') with tinker it returns my APP_URL value.

packy's avatar
Level 7

@m-rk I thought I had it in there but yes, from dev.mysite.com the command is ran and returns the correct URL from that apps env file

click's avatar

hmm I am just running through the code and I see it is retrieving the current domain from the headers of the current request:

My result of running request()->headers->all() with tinker is:

[
     "host" => [
       "test.com",
     ],
     "user-agent" => [
       "Symfony/3.X",
     ],
     "accept" => [
       "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
     ],
     "accept-language" => [
       "en-us,en;q=0.5",
     ],
     "accept-charset" => [
       "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
     ],

In your case 'host' will probably say 'localhost' the question is..... why ;-)

packy's avatar
Level 7

@m-rk Nope, my host returns the correct host mysite.com

packy's avatar
Level 7

Should I run php artisan config:cache?

click's avatar

No with that command you are caching your config.

I am pretty clueless and can't debug deeper from here. Can you make the simplest artisan command that just does:

dump(url( 'leagues/1234'));

Ah got it! http://localhost is set in this piece of code: https://github.com/laravel/framework/blob/56a58e0fa3d845bb992d7c64ac9bb6d0c24b745a/src/Illuminate/Foundation/Bootstrap/SetRequestForConsole.php#L18

It is trying to get the app.url from your config and as a fallback it is using http://localhost. So conclusion it has something todo with the config file that is or incorrect or can't be read from your artisan command (some hard cache maybe?). Do you see a file /bootstrap/cache/config.php on your production server? If so, remove it or run php artisan config:clear

packy's avatar
Level 7

@m-rk hmmmm if I ssh in and go to boostrap/cache all is see when i ls is services.php

click's avatar

How do you run this artisan command? Manually? With a scheduler? From a Queue?

If you run it with a schedule or a queue. Can you run it manually and try it again?

And further tips, just the regular debug 101:

  • Create the simplest variant you can think of (example in my previous comment). And run that. Is it still wrong?
  • See if the correct .env file is even read. Change some variable in your .env file and read it again from within an artisan command.
packy's avatar
Level 7

@m-rk maybe that bad url in the env file caused? So since it was a command getting a notification that used url it fell back to localhost but from a controller it just used the bad url since there is no fallback?

packy's avatar
Level 7

@m-rk it is run from Scheduler on forge. I can just create a new command that dumps the url and run it manually to test.

packy's avatar
Level 7

@m-rk I just made a command that sent that same notification to just me. Then uploaded it to production, SSH in and ran the command from the command line. It send me the email and correct URL.

I think this means its good and the bad URL in the env could have caused it, but I guess I will see then the scheduler runs at 11

click's avatar

I am confused. So in the end nothing was wrong then? The last time you received your email was APP_URL correctly set on your production environment?

packy's avatar
Level 7

I just ran a test on production AFTER changing the APP_URL to https://mysite.com from http:mysite.com and it worked. Not sure if that was the issue or not. I also cleared config cache. Note this just send it to myself so when the scheduler does a mass one to all the users I hope it works. Dont see why it wouldnt unless it fails at some point and defaults to Localhost for some reason

rwa's avatar

Had same issue. APP_URL was correct, but any url() in Notification would default to localhost, tried everything in this topic. Finally Full Server restart solved the issue.

edanisko's avatar

I think I got this.

If you are trying to use 1 server, the default for session driver is:

SESSION_DRIVER=file

For one server that's fine.

If you are trying to setup 2 servers, you need to use

SESSION_DRIVER=redis

Or you get 419 because the session is not on the next server in the round robin distribution.

Please or to participate in this conversation.