简体   繁体   中英

Laravel Artisan CLI doesn't set the right URL

Synopsis

I am having a little trouble understanding how Artisan works with environments and .env files.

My problem is when I generate e-mails from a task on the command line which uses the url helper I am unable to generate the correct link.

When I try to run the following commands, my e-mails contain http://localhost/<link part> instead of http://domain.local/ ; also I am working locally.

php artisan account:probation --method=notify
php artisan --env=production account:probation --method=notify
php artisan --env=local account:probation --method=notify

edit my database connections are set inside the .env and connects because it collects the e-mail address necessary to e-mail to and also the created date/time, whilst not being able to use the APP_URL as per the .env .

When these same e-mails run via the application they are set correctly with: http://domain.local/ , so now I am a little confused.

Check out the 'url' key in config/app.php . This is the value being picked up by Artisan. By default it is set to 'http://localhost' .

If you would like to specify this value using the APP_URL in your .env file, you will need to change the 'url' key in the config file to:

'url' => env('APP_URL'),

Edit

When run via the web, a valid Request object is already available. When run via Artisan, Laravel must create a fake Request object. It does this using the url defined in the config.

This value is passed to the create method on the Symfony Request object. This method uses the native parse_url method to break the value into url components. If the parsed url does not contain a host component, it will default to localhost.

Given a url of domain.local , you get the following results:

print_r(parse_url('domain.local'));
=====
Array
(
    [path] => domain.local
)

Since the components don't include a host component, the host will default to localhost.

If, however you used //domain.local , you get the following results:

print_r(parse_url('//domain.local'));
=====
Array
(
    [host] => domain.local
)

Now that the host component exists, this will be used for any URL generation required by your Artisan commands.

Source code to look at for more information:

Getting the url from the config and creating the Request :
https://github.com/laravel/framework/blob/5.0/src/Illuminate/Foundation/Bootstrap/SetRequestForConsole.php

The Symfony Request being generated ( create method called above):
https://github.com/symfony/HttpFoundation/blob/master/Request.php

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