{"id":2080,"date":"2015-11-13T22:52:18","date_gmt":"2015-11-13T21:52:18","guid":{"rendered":"http:\/\/codingexplained.com\/?p=2080"},"modified":"2017-06-11T22:00:16","modified_gmt":"2017-06-11T20:00:16","slug":"getting-url-parameters-in-zf2-controllers","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/getting-url-parameters-in-zf2-controllers","title":{"rendered":"Getting URL Parameters in ZF2 Controllers"},"content":{"rendered":"<p>Accessing URL parameters from controllers in Zend Framework 2 is extremely easy thanks to the <span class=\"code\">params<\/span> controller plugin. This plugin exposes various methods that one can use to retrieve parameters from various sources of a request, including from the URL.<\/p>\n<p>I will give examples of the various sources below.<\/p>\n<h2>Getting URL Parameters<\/h2>\n<p>To get URL parameters within Zend Framework 2 controllers, simply invoke the <span class=\"code\">fromRoute<\/span> or <span class=\"code\">fromQuery<\/span> methods on the <span class=\"code\">params<\/span> controller plugin. The <span class=\"code\">fromRoute<\/span> method should be used when you wish to retrieve a segment from a segment route for instance, while the <span class=\"code\">fromQuery<\/span> is used to fetch query parameters from the URL.<\/p>\n<pre><code class=\"php\">public function viewPostAction()\r\n{\r\n\t\/\/ Get segment from URL\r\n\t$postId = $this->params()->fromRoute('postId'); \/\/ \"postId\" = segment\r\n\t\r\n\t\/\/ Get query parameter\r\n\t$postId = $this->params()->fromQuery('postId');\r\n\t\r\n\treturn new ViewModel();\r\n}<\/code><\/pre>\n<h2>Getting POST Parameter<\/h2>\n<p>To get a POST parameter from a request, simply use the <span class=\"code\">fromPost<\/span> method on the <span class=\"code\">params<\/span> controller plugin.<\/p>\n<pre><code class=\"php\">public function viewPostAction()\r\n{\r\n\t\/\/ Get POST parameter\r\n\t$postId = $this->params()->fromPost('postId');\r\n\t\r\n\treturn new ViewModel();\r\n}<\/code><\/pre>\n<h2>Getting HTTP Headers<\/h2>\n<p>To get a given HTTP header within a Zend Framework 2 controller, simply use the <span class=\"code\">fromHeader<\/span> method on the <span class=\"code\">params<\/span> controller plugin.<\/p>\n<pre><code class=\"php\">public function viewPostAction()\r\n{\r\n\t\/\/ Get HTTP header\r\n\t$userAgent = $this->params()->fromHeader('User-Agent');\r\n\t\r\n\treturn new ViewModel();\r\n}<\/code><\/pre>\n<h2>Getting Files From Requests<\/h2>\n<p>To fetch a file from the request, one can simply use the <span class=\"code\">fromFiles<\/span> method on the <span class=\"code\">params<\/span> controller plugin.<\/p>\n<pre><code class=\"php\">public function viewPostAction()\r\n{\r\n\t\/\/ Get file from request\r\n\t$myFile = $this->params()->fromFiles('MyFile');\r\n\t\r\n\treturn new ViewModel();\r\n}<\/code><\/pre>\n<h2>Getting Parameters from any Source<\/h2>\n<p>Conveniently, one can also retrieve a parameter without specifying from where the parameter should be fetch. For instance, to retrieve a <span class=\"code\">postId<\/span> regardless of where it is present, simply do as below.<\/p>\n<pre><code class=\"php\">public function viewPostAction()\r\n{\r\n\t\/\/ Get parameter from any source\r\n\t$postId = $this->params('postId');\r\n\t\r\n\treturn new ViewModel();\r\n}<\/code><\/pre>\n<p>The above will fetch the value of a <span class=\"code\">postId<\/span> parameter if one is available within any of the request sources.<\/p>\n<h2>Specifying a Default Value<\/h2>\n<p>When fetching parameters, one can also specify a default value that should be returned if the parameter is not available. This could for instance be useful when paginating a set of items; if no <span class=\"code\">page<\/span> parameter is available, it should default to one. This avoids having to make conditional checks and keeps the code slightly more clean.<\/p>\n<pre><code class=\"php\">public function viewPostAction()\r\n{\r\n\t\/\/ Specifying default value when retrieving from any source\r\n\t$this->params('page', 1);\r\n\t\r\n\t\/\/ Specifying default value when retrieving from specific source\r\n\t$this->params()->fromRoute('page', 1);\r\n\t\r\n\treturn new ViewModel();\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Accessing URL parameters from controllers in Zend Framework 2 is extremely easy thanks to the params controller plugin. This plugin exposes various methods that one can use to retrieve parameters from various sources of a request, including from the URL. I will give examples of the various sources below. Getting URL Parameters To get URL&hellip; <a href=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/getting-url-parameters-in-zf2-controllers\" class=\"more-link\">read more<\/a><\/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":true,"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>Getting URL Parameters in ZF2 Controllers<\/title>\n<meta name=\"description\" content=\"Getting URL parameters within controllers is easy in Zend Framework 2. See how request parameters can be accessed from various sources.\" \/>\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\/getting-url-parameters-in-zf2-controllers\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Getting URL Parameters in ZF2 Controllers\" \/>\n<meta property=\"og:description\" content=\"Getting URL parameters within controllers is easy in Zend Framework 2. See how request parameters can be accessed from various sources.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/getting-url-parameters-in-zf2-controllers\" \/>\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=\"2015-11-13T21:52:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T20:00:16+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\/getting-url-parameters-in-zf2-controllers\",\"url\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/getting-url-parameters-in-zf2-controllers\",\"name\":\"Getting URL Parameters in ZF2 Controllers\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2015-11-13T21:52:18+00:00\",\"dateModified\":\"2017-06-11T20:00:16+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Getting URL parameters within controllers is easy in Zend Framework 2. See how request parameters can be accessed from various sources.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/getting-url-parameters-in-zf2-controllers#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/getting-url-parameters-in-zf2-controllers\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/getting-url-parameters-in-zf2-controllers#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Getting URL Parameters in ZF2 Controllers\"}]},{\"@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":"Getting URL Parameters in ZF2 Controllers","description":"Getting URL parameters within controllers is easy in Zend Framework 2. See how request parameters can be accessed from various sources.","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\/getting-url-parameters-in-zf2-controllers","og_locale":"en_US","og_type":"article","og_title":"Getting URL Parameters in ZF2 Controllers","og_description":"Getting URL parameters within controllers is easy in Zend Framework 2. See how request parameters can be accessed from various sources.","og_url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/getting-url-parameters-in-zf2-controllers","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2015-11-13T21:52:18+00:00","article_modified_time":"2017-06-11T20:00:16+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\/getting-url-parameters-in-zf2-controllers","url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/getting-url-parameters-in-zf2-controllers","name":"Getting URL Parameters in ZF2 Controllers","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2015-11-13T21:52:18+00:00","dateModified":"2017-06-11T20:00:16+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Getting URL parameters within controllers is easy in Zend Framework 2. See how request parameters can be accessed from various sources.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/getting-url-parameters-in-zf2-controllers#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/php\/zend-framework\/getting-url-parameters-in-zf2-controllers"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/getting-url-parameters-in-zf2-controllers#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Getting URL Parameters in ZF2 Controllers"}]},{"@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-xy","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/2080"}],"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=2080"}],"version-history":[{"count":5,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/2080\/revisions"}],"predecessor-version":[{"id":3019,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/2080\/revisions\/3019"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=2080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=2080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=2080"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=2080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}