Getting Hostname in Laravel
- 2021年8月02日
- 技術情報
We have to catch your domain name, hostname and url segments and use it back in your projects frequently. Today I will share a few functions to get it done easily in Laravel Framework.
getHttpHost
request()->getHttpHost(); //return domain.com
This will return the domain name like this.
request()->getHost(); // return domain.com
This also works same with above method.
getSchemeAndHttpHost
If you want to get http segments along with the domain name. You can use getSchemeAndHttpHost method.
request()->getSchemeAndHttpHost(); // return https://domain.com
url
Using the url method, you can get the current route path like this.
url()->current(); // return current existing url
You can also use request()->url()
to return the url address along with http.
segment
If you need to parse your routing url , the segment will help to parse it. For example this is our current url domain.com/blog/hello
request()->segment(0); // return blog
request()->segment(1); // return hello
The methods I wrote above are some of the useful functions if you have to deal with your URL address or hostname.
Yuuma
yuuma at 2021年08月02日 10:30:20