Home > front end >  How to overwrite route() functionality in Laravel 9.x
How to overwrite route() functionality in Laravel 9.x

Time:07-15

I made webpage with Laravel which next I put on private server. After doing that I found out that my links to named routes aren't working.

After online research and talking with server administrator I learned that route() helper in Laravel is using request domain to build links, and that this server will always give me IP address instead of domain and that it is impossible on this server to access anything via IP address, it needs to be via domain.

Because of need to quick dealing with the problem I temporary made custom helper that using route() inside of it and changing IP address in result to app domain (taken from config). It works fine but I can't use third party libraries thanks to that. And I don't like it.

I tought about using middleware on whole app to change that IP address in request on domain but I have no idea (And I couldnt find it in Google) how to do that so route() helper would read it properly. Can you give me any ideas about that?

Thanks in advance.

CodePudding user response:

You can do this little hack if your APP_URL env variable is not working for whatever reason. In your AppServiceProvider boot function add the following:

$this->app->resolving(UrlGenerator::class, function (UrlGenerator $generator) {
     $generator->forceRootUrl(env('APP_URL')); 
});

This should force a new root url when resolving the url generator.

  • Related