Getting Hostname in Laravel

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



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム