framework: Getting url() in a Command returns http://localhost

I’m trying to get my base url (i.e. http://localhost/l4/public) from a Command but it returns http://localhost instead.

The functions url() and URL::to('/') gets a blank base url from SymfonyRequest when accessing from a Command even if a url is defined in app/config/local/app.php.

However, URL::current() works as expected.

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

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'));
}

This will force the URL for all generated routes/actions throughout the application, for example using:

route('test.show', [$this->getKey()])

That type of “sub-folder” setup is not support by Laravel in URLs.

Setting a forced root url does not work for queuing mails 😕

You need to set the url in app.url

I was having a headache from this route problem, I’m using Laravel 5.4. @mstephens Thank’s for your solution.

In my case the issue was with the environment setting. I tried to set ENV at end of command and URL worked fine. php artisan queue:listen --env=live

(with laravel 4.2)

first set url in app/config/app.php then include config in your model use Config;

then try echo Config::get(‘app.url’);