Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

route() helper function does not acknowledge app.url #18613

Closed
mikefrancis opened this issue Apr 1, 2017 · 14 comments
Closed

route() helper function does not acknowledge app.url #18613

mikefrancis opened this issue Apr 1, 2017 · 14 comments

Comments

@mikefrancis
Copy link

  • Laravel Version: 5.4.15
  • PHP Version: 7.1.2-2+deb.sury.org~xenial+1
  • Database Driver & Version: 5.7.17-0ubuntu0.16.04.1

Description:

I am using the route($name) helper function in my views to take advantage of named routes, however if I try to change the app.url in config/app.php (APP_URL environment variable) this doesn't change the prefix of any of the URLs generated by route($name). I discovered this when using an ngrok tunnel's URL to try to demo something I was working on to a colleague.

Steps To Reproduce:

  • Change app.url in config/app.php to something other than your hostname
  • Call route($name) from a view
  • See URL does not equal updated URL and instead equals hostname

Notes

I have done some debugging and this is being generated in here:

https://github.com/symfony/http-foundation/blob/master/Request.php#L1244

While this is a Symfony file, it would be amazing if Laravel honoured the app.url when generating URLs.

This might get shot down as I imagine it's a lot more secure to generate URLs from the hostname rather than some user inputted value but just thought I would float this and see if it's viable.

Thanks!

@brunogaspar
Copy link
Contributor

The app.url is only used on CLI as far as i know.

@vlakoff
Copy link
Contributor

vlakoff commented Apr 2, 2017

Probably related: #14139

@srmklive
Copy link
Contributor

srmklive commented Apr 3, 2017

I think if you don't define the APP_URL in your .env file, it will automatically get the new URL whenever you change it.

Try clearing the cache first, and then check.

@themsaid
Copy link
Member

themsaid commented Apr 3, 2017

The app.url is only used on CLI as far as i know.

Yes that's the main purpose of it.

@mikefrancis
Copy link
Author

So if app.url is used for the CLI, then there should be no (or the same) implications for using this for routing too?

@sisve
Copy link
Contributor

sisve commented Apr 5, 2017

Changing the UrlGenerator/RouteUrlGenerator to using app.url would result in all visitors being moved onto one domain; the one specified in app.url. The current behavior is to keep the user on the current domain. Changing this would cause issues for those of us that are running a multi-host setup where we have visitors on many domains.

@themsaid themsaid closed this as completed May 5, 2017
@vesper8
Copy link

vesper8 commented Nov 2, 2017

this is lacking flexibility however. There should at least be a way to force route() to use a given url for cases where it's needed.. one that doesn't involve doing a str_replace on the default route

@sisve
Copy link
Contributor

sisve commented Nov 5, 2017

@vesper8 Yes. Url::forceRoot('https://www.google.com/')

@vesper8
Copy link

vesper8 commented Nov 6, 2017

Thanks @sisve !

And it's actually URL::forceRootUrl("https://www.google.com");

@ryancwalsh
Copy link

ryancwalsh commented Feb 8, 2019

I was having problems because I was using Ngrok and the Laravel login.blade.php wanted to post to route('login'), which wasn't honoring my APP_URL.

The best hint I found was to not use -host-header=rewrite in Ngrok: https://stackoverflow.com/a/45995667/470749

So then I called .\ngrok http kvb.test:80 instead of .\ngrok http -host-header=rewrite kvb.test:80

Of course, this then meant that I needed to be careful about which of my many local apps was considered the default local Homestead app.

So I created an entry higher up in Homestead.yaml with "aaaaaa.test" for the project I was working on.

Then /login worked at my Ngrok URL.

@ryancwalsh
Copy link

I'm still having trouble; I don't think my weird steps posted above in February are the right approach.

I found a related issue (#19556) but no solution yet.

@monaye
Copy link
Contributor

monaye commented Mar 6, 2020

I think UrlGenerator/RouteUrlGenerator to use the app.url should be the default behavior and overwrite if it needs to be different.

Changing the UrlGenerator/RouteUrlGenerator to using app.url would result in all visitors being moved onto one domain; the one specified in app.url. The current behavior is to keep the user on the current domain. Changing this would cause issues for those of us that are running a multi-host setup where we have visitors on many domains.

@reduardo7
Copy link

reduardo7 commented Oct 5, 2020

@vesper8 Yes. Url::forceRoot('https://www.google.com/')

Thank you @sisve !

I put the suggestion at my routes/api.php:

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;

Route::name('api')
  ->middleware([
    'cors',
    'json.response',
    'transactional',
  ])
  ->group(function () {
    // https://github.com/laravel/framework/issues/18613#issuecomment-341991605
    $url = config('app.url');
    URL::forceRootUrl($url);

    // ...

@BippyMiester
Copy link

@vesper8 Yes. Url::forceRoot('https://www.google.com/')

Thank you @sisve !

I put the suggestion at my routes/api.php:

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;

Route::name('api')
  ->middleware([
    'cors',
    'json.response',
    'transactional',
  ])
  ->group(function () {
    // https://github.com/laravel/framework/issues/18613#issuecomment-341991605
    $url = config('app.url');
    URL::forceRootUrl($url);

    // ...

Yes, this functionality worked for me. For some reason my BrowserSync on this one specific route view was not using port 3000 for all my links, but rather port 8000. Everything else was working just fine. So i shoved this at the top of my web.php routes file and it works perfectly! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests