laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.27k stars 10.94k forks source link

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

Closed zgosalvez closed 10 years ago

zgosalvez commented 10 years ago

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.

Anahkiasen commented 10 years ago

You need to set the url in app.url

zgosalvez commented 10 years ago

@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.

anlutro commented 10 years ago

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.

crynobone commented 10 years ago

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.

harryxu commented 10 years ago

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

taylorotwell commented 10 years ago

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

garygreen commented 10 years ago

@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

crynobone commented 10 years ago

@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.

rameezrami commented 9 years ago

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

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

mossen commented 8 years ago

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/

LasseRafn commented 8 years ago

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

shinmao commented 8 years ago

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

mstephens commented 8 years ago

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()])
LasseRafn commented 8 years ago

Cool! Thanks :-) I'll give it a try when I'm home On Tue, 13 Sep 2016 at 12.53, Matt notifications@github.com 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

ianrussel commented 7 years ago

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

rameezrami commented 7 years ago

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)

garbinmarcelo commented 7 years ago

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

gamalNasserYSF commented 6 years ago

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