php - 更改 Laravel 4.2 Artisan 命令中的应用程序 url

标签 php laravel laravel-4 laravel-artisan

我运行一个用 Laravel 4.2 构建的 Multi-Tenancy 站点。在应用程序配置中,我知道您可以设置单个基本 URL,例如

/*
|--------------------------------------------------------------------------
| Application URL
|--------------------------------------------------------------------------
|
| This URL is used by the console to properly generate URLs when using
| the Artisan command line tool. You should set this to the root of
| your application so that it is used when running Artisan tasks.
|
*/

'url' => 'http://apples.local',

我构建了一个 Artisan 命令来向用户发送预定的电子邮件,无论他们通过哪个域访问该网站。因此该命令需要生成具有不同域的 url,例如http://oranges.local

在我的命令中,我尝试在生成 URL 之前更改 app.url 配置变量,但它似乎没有影响:

Config::set('app.url', 'http://oranges.local');
$this->info('App URL: ' . Config::get('app.url'));
$this->info('Generated:' . URL::route('someRoute', ['foo', 'bar']));

尽管在运行时物理地改变了配置,这仍然总是产生:

App URL: http://oranges.local
Generated: http://apples.local/foo/bar

URL 生成器完全忽略应用程序配置!

我知道我可以设置多个环境并将 --env=oranges 传递给 Artisan,但在我的用例中这并不实际。我只希望能够在运行时在站点范围内设置应用程序 URL。

有什么想法吗?

最佳答案

好吧,whoacowboy 是对的,配置根本没有被考虑。我发现该网站的基本 URL(即使在 Artisan 命令中)似乎是从 Symphony Request 对象一路返回的。

因此,要更改/欺骗 Artisan 内的域,可以执行以下操作:

Request::instance()->headers->set('host', 'oranges.local');

完成更改后再次恢复主机名可能是明智之举,但至少在我的用例中,这已经解决了我的所有问题!

关于php - 更改 Laravel 4.2 Artisan 命令中的应用程序 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33102926/

相关文章:

php - 如何在我的开发服务器上启用通知

php - Laravel:如何检查用户是否是管理员?

php - 我可以将模型关联到 laravel 中的通知表吗?

php - 从多个表中排序数据

javascript - 将 id 值传递给 ajax 页面

php - Doctrine 2 : Is there a way to inherit mapping from a trait using yaml or xml?

laravel - 属于多个关系。如何获得唯一的行

php - $this ['key' ]->method() - 这是什么?

php - 带 Bootstrap 日期选择器的 Laravel 4

php - 在 Laravel 4 中使用 Sphinx