简体   繁体   中英

Laravel 4 url() method is not working in command line

I am running a cron that sends an email where i am sending application link to the user. In my config file i have set the application url as 'url()', that gives me the correct route to the application's public path in browser. But the same thing is giving only 'localhost' when i run it in cron.

How can i get the application url in command line ?

Instead of using url() in one circumstance and a config value in another, you can also use URL::forceRootUrl() just once, up front, and then use url() everywhere.

For example, if you do this once at the start of your command's code:

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

...then that's what url() will return from then on. Also, other helpers like action() , etc. will use that as their base and return what you'd expect.

If you are running your php script through cron by setting up your laravel artisan command, you are not expected to get any SEVER execution environment information nor HTTP agent info unlike when you run your script through browser. Your function 'url()' internally gets the info about public url from [HTTP_HOST]/[SERVER_NAME] of SERVER variable which generally not present if you run your php script from cron(through artisan command).

In this case if you really need your app's public url, you can set this in your included config file and use in this case and your url() function in any other cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM