{"id":560,"date":"2013-03-10T21:07:02","date_gmt":"2013-03-10T20:07:02","guid":{"rendered":"http:\/\/codingexplained.com\/?p=560"},"modified":"2017-06-11T22:18:40","modified_gmt":"2017-06-11T20:18:40","slug":"create-full-urls-in-zf2-views","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views","title":{"rendered":"Create Full URLs in ZF2 Views"},"content":{"rendered":"<p>Sometimes it is useful to create full URLs within view scripts. For instance, if you are using view scripts to generate markup to be used in e-mails, you cannot link to your website by using relative links. The <span class=\"code\">Zend\\View\\Helper\\Url<\/span> helper generates relative URLs by default. This can, however, be changed by providing an option in its third parameter.<\/p>\n<pre><code class=\"php\">\/\/ Using a route with the name \"register\" and the route \"\/register\"\r\necho $this->url('register', array(), array('force_canonical' => true)); \/\/ Output: http:\/\/mydomain.com\/register<\/code><\/pre>\n<p>Well, that was easy! But what if we do not want to generate an URL for a route? Luckily, the <span class=\"code\">Zend\\View\\Helper\\ServerUrl<\/span> view helper can help us out. This view helper is not mentioned within the official documentation, so it can be quite easy to miss. It even provides a mechanism for automatically detecting the schema (usually <span class=\"code\">http<\/span> or <span class=\"code\">https<\/span>) if we do not wish to specify this explicitly. Either way, its usage is simple:<\/p>\n<pre><code class=\"php\">echo $this->serverUrl(); \/\/ Output: http:\/\/mydomain.com<\/code><\/pre>\n<p>The view helper&#8217;s <span class=\"code\">__invoke<\/span> magic method takes an optional parameter. If the boolean value <span class=\"code\">TRUE<\/span> is passed, it returns the server URL with the <span class=\"code\">$_SERVER<\/span>&#8216;s request URI appended. Consider the following example:<\/p>\n<pre><code class=\"php\">\/\/ Current URL: http:\/\/mydomain.com\/register\r\necho $this->serverUrl(true); \/\/ Output: http:\/\/mydomain.com\/register<\/code><\/pre>\n<p>If passing a string, the helper will simply append that string to the end of the generated URL. By using a combination of the two mentioned view helpers, we can create an URL identical to the one in the first example:<\/p>\n<pre><code class=\"php\">\/\/ The \"register\" route has the following route: \/register\r\necho $this->serverUrl($this->url('register')); \/\/ Output: http:\/\/mydomain.com\/register<\/code><\/pre>\n<p>In most situations, it would probably make more sense to just use the <span class=\"code\">Zend\\View\\Helper\\Url<\/span> helper for this, but that is of course entirely up to you and your personal preference. On the other hand, if you need the additional flexibility that the <span class=\"code\">serverUrl<\/span> helper provides in terms of manipulating the scheme and host, then this is indeed not a bad solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating full URLs within view scripts can come in handy in many situations, such as when sending out e-mails where one wants to link back to one&#8217;s website. Luckily, Zend Framework 2 provides two useful view helpers to accomplish this task.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[38],"tags":[40,39,66],"series":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create Full URLs in ZF2 Views<\/title>\n<meta name=\"description\" content=\"Creating full URLs within view scripts in Zend Framework 2 is very easy. In fact, there are two view helpers available depending on the use case.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create Full URLs in ZF2 Views\" \/>\n<meta property=\"og:description\" content=\"Creating full URLs within view scripts in Zend Framework 2 is very easy. In fact, there are two view helpers available depending on the use case.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views\" \/>\n<meta property=\"og:site_name\" content=\"Coding Explained\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:published_time\" content=\"2013-03-10T20:07:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T20:18:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"444\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Bo Andersen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:site\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bo Andersen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views\",\"url\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views\",\"name\":\"Create Full URLs in ZF2 Views\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2013-03-10T20:07:02+00:00\",\"dateModified\":\"2017-06-11T20:18:40+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Creating full URLs within view scripts in Zend Framework 2 is very easy. In fact, there are two view helpers available depending on the use case.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create Full URLs in ZF2 Views\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codingexplained.com\/#website\",\"url\":\"https:\/\/codingexplained.com\/\",\"name\":\"Coding Explained\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codingexplained.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\",\"name\":\"Bo Andersen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"caption\":\"Bo Andersen\"},\"description\":\"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!\",\"sameAs\":[\"https:\/\/codingexplained.com\",\"https:\/\/www.facebook.com\/codingexplained\",\"https:\/\/www.linkedin.com\/in\/ba0708\",\"https:\/\/twitter.com\/codingexplained\",\"https:\/\/www.youtube.com\/c\/codingexplained\"],\"url\":\"https:\/\/codingexplained.com\/author\/andy\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create Full URLs in ZF2 Views","description":"Creating full URLs within view scripts in Zend Framework 2 is very easy. In fact, there are two view helpers available depending on the use case.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views","og_locale":"en_US","og_type":"article","og_title":"Create Full URLs in ZF2 Views","og_description":"Creating full URLs within view scripts in Zend Framework 2 is very easy. In fact, there are two view helpers available depending on the use case.","og_url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2013-03-10T20:07:02+00:00","article_modified_time":"2017-06-11T20:18:40+00:00","og_image":[{"width":1200,"height":444,"url":"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png","type":"image\/png"}],"author":"Bo Andersen","twitter_card":"summary_large_image","twitter_creator":"@codingexplained","twitter_site":"@codingexplained","twitter_misc":{"Written by":"Bo Andersen","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views","url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views","name":"Create Full URLs in ZF2 Views","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2013-03-10T20:07:02+00:00","dateModified":"2017-06-11T20:18:40+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Creating full URLs within view scripts in Zend Framework 2 is very easy. In fact, there are two view helpers available depending on the use case.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/create-full-urls-in-zf2-views#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Create Full URLs in ZF2 Views"}]},{"@type":"WebSite","@id":"https:\/\/codingexplained.com\/#website","url":"https:\/\/codingexplained.com\/","name":"Coding Explained","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codingexplained.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d","name":"Bo Andersen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","caption":"Bo Andersen"},"description":"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!","sameAs":["https:\/\/codingexplained.com","https:\/\/www.facebook.com\/codingexplained","https:\/\/www.linkedin.com\/in\/ba0708","https:\/\/twitter.com\/codingexplained","https:\/\/www.youtube.com\/c\/codingexplained"],"url":"https:\/\/codingexplained.com\/author\/andy"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3mJkW-92","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/560"}],"collection":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/comments?post=560"}],"version-history":[{"count":5,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/560\/revisions"}],"predecessor-version":[{"id":3041,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/560\/revisions\/3041"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=560"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}