简体   繁体   中英

URL::asset() with php artisan command gives me localhost links

I try to send some emails with a CLI command with php artisan of Laravel, something easy like :

 php artisan invitation:send recipient@mail.com

This command is calling a UserController method which contains :

Mail::send('emails.beta.invitation', $data, function($message) use ($address)
{
    $message->to($address)
            ->subject('My subject');

});

The problem is that when it creates the HTML using the view, all references to URL::asset('img/foo.png') in the template gives me a beautiful :

http://localhost/img/foo.png 

instead of the website url :

http://mydomain.com/img/foo.png

If I call this method by calling it in the web browser, there is the good URI for the asset. I even tried with an environment to the CLI but it doesn't work. (ie. --env=production )

Where am I wrong ?

All right, I got it.

When using the CLI, Laravel is using the config file app/config/app.php to read the 'url' (default 'url' => 'http://localhost' ).

So I just had to create a new config file under app/config/local/app.php with :

<?php
return array(
    'url' => 'http://localhost:8000',
);

And to change the app/config/app.php with my production value :

'url' => 'http://mydomain.com'

Now it works well !

https://github.com/laravel/framework/issues/2554#issuecomment-246645265

mstephens commented on 13 Sep 2016 Hello

In App\\Providers\\RouteServiceProvider you can define the following:

/**
 * @inheritdoc
 */
public function boot()
{
    parent::boot();

    /** @var \Illuminate\Routing\UrlGenerator $url */
    $url = $this->app['url'];
    // Force the application URL
    $url->forceRootUrl(config('app.url'));
}

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