URL::asset() 带有 php artisan 命令给了我本地主机链接


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

我尝试使用带有 php artisan of Laravel 的 CLI 命令发送一些电子邮件,例如:

 php artisan invitation:send recipient@mail.com

此命令正在调用包含以下内容的用户控制器方法:

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

问题是,当它使用视图创建 HTML 时,模板中对URL::asset('img/foo.png')的所有引用都给了我一个漂亮的:

http://localhost/img/foo.png 

而不是网站网址:

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

如果我通过在 Web 浏览器中调用此方法来调用此方法,则资产具有良好的 URI。我什至尝试将环境用于 CLI,但它不起作用。(即。 --env=production

我错在哪里?

好吧,我知道了。

使用 CLI 时,Laravel 正在使用配置文件app/config/app.php读取"url"(默认'url' => 'http://localhost')。

所以我只需要在 app/config/local/app.php 下创建一个新的配置文件:

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

并用我的产值改变app/config/app.php

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

现在它运行良好!

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

斯蒂芬斯于2016年9月13日评论你好

在 App''Providers''RouteServiceProvider 中,可以定义以下内容:

/**
 * @inheritdoc
 */
public function boot()
{
    parent::boot();
    /** @var 'Illuminate'Routing'UrlGenerator $url */
    $url = $this->app['url'];
    // Force the application URL
    $url->forceRootUrl(config('app.url'));
}