{"id":1664,"date":"2024-10-21T14:39:30","date_gmt":"2024-10-21T13:39:30","guid":{"rendered":"https:\/\/editjournal.redakt.eu\/faxmodem\/?p=1664"},"modified":"2024-10-21T14:39:30","modified_gmt":"2024-10-21T13:39:30","slug":"optional-parameter-in-named-routes","status":"publish","type":"post","link":"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/development\/cakephp\/quick-bites\/optional-parameter-in-named-routes\/","title":{"rendered":"Optional parameter in named routes"},"content":{"rendered":"<p>Let's say you want to make a named CakePHP route which may or may not have an argument. For example, you want to return WHOIS information for an IP address:<\/p>\n<ul>\n<li><code>\/whois\/1.1.1.1<\/code> returns WHOIS information for <code>1.1.1.1<\/code> which is specifically passed in the URL;<\/li>\n<li><code>\/whois<\/code> will return WHOIS information for the visitor's IP address.<\/li>\n<\/ul>\n<p>When doing a quick search, I found <a href=\"https:\/\/github.com\/cakephp\/cakephp\/issues\/14628\">this somewhat related Github issue<\/a>, where devs said it's not possible and suggested OP defines two separate routes instead: one with the argument and the other one without it.<\/p>\n<p>The problem with a named route is that <a href=\"https:\/\/book.cakephp.org\/5\/en\/development\/routing.html#using-named-routes\">names must be unique, same name cannot be used twice<\/a>.<\/p>\n<p>Here's the working config for a named route that accepts an optional argument and if it's not present, defaults it to empty.<\/p>\n<p>In your <code>config\/routes.php<\/code>:<\/p>\n<pre><code>$routes-&gt;connect(\n    &#039;\/whois\/{ip}&#039;,\n    [\n        \/\/&#039;plugin&#039; =&gt; $this-&gt;getName(),\/\/ use if you&#039;re inside a plugin\n        \/\/&#039;prefix&#039; =&gt; &#039;Admin&#039;,\/\/use if route needs to be inside a prefix\n        &#039;controller&#039; =&gt; &#039;Whois&#039;,\n        &#039;action&#039; =&gt; &#039;getinfo&#039;,\n        &#039;ip&#039; =&gt; &#039;&#039;, \/\/ defaults to empty\n    ],\n    [\n        &#039;_name&#039; =&gt; &#039;ipwhois&#039;,\n    ]\n)-&gt;setPass([&#039;ip&#039;]);<\/code><\/pre>\n<p>Now you can call this route in your scripts elsewhere in the application:<\/p>\n<ul>\n<li><code>echo \\Cake\\Routing\\Router::url([&#039;_name&#039; =&gt; &#039;ipwhois&#039;]);<\/code><\/li>\n<li><code>echo \\Cake\\Routing\\Router::url([&#039;_name&#039; =&gt; &#039;ipwhois&#039;, &#039;ip&#039; =&gt; $this-&gt;getRequest()-&gt;clientIp()]);<\/code><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you want to make a named CakePHP route which may or may not have an argument. For example, you want to return WHOIS information for an IP address: \/whois\/1.1.1.1 returns WHOIS information for 1.1.1.1 which is specifically passed in the URL; \/whois will return WHOIS information for the visitor&#8217;s IP address. When doing&hellip; <a class=\"more-link\" href=\"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/development\/cakephp\/quick-bites\/optional-parameter-in-named-routes\/\">Continue reading <span class=\"screen-reader-text\">Optional parameter in named routes<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1234],"tags":[],"class_list":["post-1664","post","type-post","status-publish","format-standard","hentry","category-quick-bites","entry"],"_links":{"self":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/comments?post=1664"}],"version-history":[{"count":1,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1664\/revisions"}],"predecessor-version":[{"id":1665,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1664\/revisions\/1665"}],"wp:attachment":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/media?parent=1664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/categories?post=1664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/tags?post=1664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}