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.
app.url
in config/app.php
to something other than your hostnameroute($name)
from a viewI 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!
The app.url
is only used on CLI as far as i know.
Probably related: #14139
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.
The app.url is only used on CLI as far as i know.
Yes that's the main purpose of it.
So if app.url
is used for the CLI, then there should be no (or the same) implications for using this for routing too?
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.
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
@vesper8 Yes. Url::forceRoot('https://www.google.com/')
Thanks @sisve !
And it's actually URL::forceRootUrl("https://www.google.com");
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.
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 (https://github.com/laravel/framework/issues/19556) but no solution yet.
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.
@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);
// ...
@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!
Most helpful comment
Thanks @sisve !
And it's actually
URL::forceRootUrl("https://www.google.com");