使用php artisan命令的URL :: asset()为我提供了localhost链接

问题描述 投票:3回答:2

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

 php artisan invitation:send [email protected]

此命令调用UserController方法,该方法包含:

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

我哪里错了?

php laravel laravel-4
2个回答
8
投票

好吧,我明白了。

使用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'

现在它运作良好!


0
投票

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

mstephens于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'));
}
© www.soinside.com 2019 - 2024. All rights reserved.