{"id":10314,"date":"2021-08-02T10:30:20","date_gmt":"2021-08-02T01:30:20","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=10314"},"modified":"2021-07-30T21:31:21","modified_gmt":"2021-07-30T12:31:21","slug":"getting-hostname-in-laravel","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/10314","title":{"rendered":"Getting Hostname in Laravel"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<h3>getHttpHost<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>request()->getHttpHost(); \/\/return domain.com<\/code><\/pre>\n\n\n\n<p>This will return the domain name like this. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>request()->getHost();\u00a0\/\/ return domain.com<\/code><\/pre>\n\n\n\n<p>This also works same with above method.<\/p>\n\n\n\n<h3>getSchemeAndHttpHost<\/h3>\n\n\n\n<p>If you want to get http segments along with the domain name. You can use getSchemeAndHttpHost method. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>request()->getSchemeAndHttpHost(); \/\/ return https:\/\/domain.com<\/code><\/pre>\n\n\n\n<h3>url<\/h3>\n\n\n\n<p>Using the url method, you can get the current route path like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>url()->current(); \/\/ return current existing url<\/code><\/pre>\n\n\n\n<p>You can also use <code>request()-&gt;url()<\/code> to return the url address along with http.<\/p>\n\n\n\n<h3>segment<\/h3>\n\n\n\n<p>If you need to parse your routing url , the segment will help to parse it. For example this is our current url <code>domain.com\/blog\/hello<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>request()->segment(0); \/\/ return blog\nrequest()->segment(1); \/\/ return hello <\/code><\/pre>\n\n\n\n<p>The methods I wrote above are some of the useful functions if you have to deal with your URL address or hostname.<\/p>\n\n\n\n<p>Yuuma<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10314\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/10314\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Getting Hostname in Laravel\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10314\" data-text=\"Getting Hostname in Laravel\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10314\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/10314\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>We have to catch your domain name, hostname and url segments and use it back in your projects frequently. Toda [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10314"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=10314"}],"version-history":[{"count":3,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10314\/revisions"}],"predecessor-version":[{"id":10318,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/10314\/revisions\/10318"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=10314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=10314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=10314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}