使用php artisan命令的URL :: asset()为我提供了localhost链接
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用php artisan命令的URL :: asset()为我提供了localhost链接相关的知识,希望对你有一定的参考价值。
我尝试使用带有php artisan of Laravel的CLI命令发送一些电子邮件,这很简单:
php artisan invitation:send recipient@mail.com
此命令调用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
)
我哪里错了?
好吧,我明白了。
使用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
mstephens于2016年9月13日评论您好
在App Providers RouteServiceProvider中,您可以定义以下内容:
/**
* @inheritdoc
*/
public function boot()
{
parent::boot();
/** @var IlluminateRoutingUrlGenerator $url */
$url = $this->app['url'];
// Force the application URL
$url->forceRootUrl(config('app.url'));
}
以上是关于使用php artisan命令的URL :: asset()为我提供了localhost链接的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Artisan 命令 (PHP) 在 Laravel 5.2 中创建 RESTful 资源控制器
运行任何 php artisan 命令时出现 PHP laravel 错误
如何从命令的“php artisan list”中隐藏默认的 artisan 命令?
Laravel Artisan CLI 没有设置正确的 URL