Laravel 4 url ​​() method not working on command line

I run cron, which sends an email, where I send the link to the application to the user. In my configuration file, I set the application url as 'url ()', which gives me the correct route to the public path of the application in the browser. But only "localhost" gives the same thing when I run it in cron.

How can I get the application url on the command line?

+5
source share
2 answers

If you run the PHP script through cron by setting up the larvel artisan command, you should not receive information about the execution of the SEVER environment or information about the HTTP agent, unlike when you run your browser script through the browser. Your function 'url ()' internally retrieves public URL information from [HTTP_HOST] / [SERVER_NAME] from the SERVER variable, which is usually missing if you run your php script from cron (via the wizard command).

In this case, if you really need the public URL of your application, you can set it in the attached configuration file and use your url () function in this case in any other cases.

+4
source

Instead of using url() in one case and configuration values ​​in another, you can also use URL::forceRootUrl() only once, up, and then use url() everywhere.

For example, if you do this once at the beginning of the command code:

 URL::forceRootUrl("http://example.whatever"); 

... then that url() will return from now on. In addition, other helpers, such as action() , etc., will use this as their base and return what you expect.

+1
source

Source: https://habr.com/ru/post/1202058/


All Articles