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

Created on 25 Oct 2013  Â·  18Comments  Â·  Source: laravel/framework

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.

Most helpful comment

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()])

All 18 comments

You need to set the url in app.url

@Anahkiasen , local app.url is set to http://localhost/l4/public while default app.url is http://l4.dev. So I guess it uses the local config correctly but only returns the host/domain.

Though I don't see why you'd want to use URL::to or similar methods in the console, I guess the issue can be solved by making the UrlGenerator class check if it's running in console. If it is, get the root url from the config instead of the request.

Though I don't see why you'd want to use URL::to or similar methods in the console.

Using Mail::queue() (which will be run via php artisan queue:work) and wanting to post a link to your app is one example, but I never experience an issue with it. But to be honest I'm not clear with what is the actual and expected behavior that the OP is experiencing.

I have the same issue, the app.url only effect the host name on command line and the sub path will be ignored.

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

@crynobone I have a similar issue with queue workers. How is your setup for urls to output to the correct path? It seems for app.url it ignores anything after the / as described above. When generating URL::to for requests it uses the request url and not the app.url which causes issue when pointing to i.e. your local dev machine with multiple folders i.e. /project1/public /project2/public

@garygreen I can't comment on your case when I don't know how you setup your environment. Basically I'm using the app.url config without any tweak and ensure that artisan queue:listen use the exact environment.

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

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

do this:
set your url in app/config/app.php

then initiate URL:
URL::forceRootUrl(Config::get('app.url'));

so you will get the URL by:
url('/);

for your reference: http://clivern.com/laravel-url-generation/

Setting a forced root url does not work for queuing mails :/

so can you tell me how you solve this problem in the end?
Thx

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()])

Cool! Thanks :-) I'll give it a try when I'm home
On Tue, 13 Sep 2016 at 12.53, Matt [email protected] wrote:

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()])

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/laravel/framework/issues/2554#issuecomment-246645265,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACkJPTEjJ4JxrFALRpciMCvmpNskDgvwks5qpoCegaJpZM4BImzA
.

Mvh
Lasse Rafn

how did you solve this?i have also had the same problem

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)

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

also, you can send APP URL to your view as a parameter
eg:'url' => \URL::to('auth/verifyMail/'),

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JamborJan picture JamborJan  Â·  3Comments

kerbylav picture kerbylav  Â·  3Comments

felixsanz picture felixsanz  Â·  3Comments

gabriellimo picture gabriellimo  Â·  3Comments

progmars picture progmars  Â·  3Comments