{"id":82,"date":"2011-09-28T01:29:52","date_gmt":"2011-09-28T01:29:52","guid":{"rendered":"http:\/\/info.ffteixeira.net\/2011\/09\/28\/cakephp-routing\/"},"modified":"2011-09-28T01:29:52","modified_gmt":"2011-09-28T01:29:52","slug":"cakephp-routing","status":"publish","type":"post","link":"https:\/\/blog.ffteixeira.net\/?p=82","title":{"rendered":"CakePHP Routing"},"content":{"rendered":"<p>By setting <code>'prefix' =&gt; 'admin'<\/code> you are telling CakePHP that you want to use a prefix of <code>admin<\/code> for that route; basically meaning you want to use controller actions and views that have names prefixed with <code>admin_<\/code>. This part you are already aware of, and things will probably work fine with just this.<\/p>\n<p>When creating routes though, any array keys passed into the second argument that aren&#8217;t recognised by CakePHP (ie. not your usual <code>controller<\/code>, <code>action<\/code>, <code>plugin<\/code>, <code>prefix<\/code> stuff) are set as named parameters during requests matching that route.<\/p>\n<p><!--more--><br \/>\nBy setting <code>'prefix' =&gt; 'admin'<\/code> you are telling CakePHP that you want to use a prefix of <code>admin<\/code> for that route; basically meaning you want to use controller actions and views that have names prefixed with <code>admin_<\/code>. This part you are already aware of, and things will probably work fine with just this.<\/p>\n<p>When creating routes though, any array keys passed into the second argument that aren&#8217;t recognised by CakePHP (ie. not your usual <code>controller<\/code>, <code>action<\/code>, <code>plugin<\/code>, <code>prefix<\/code> stuff) are set as named parameters during requests matching that route.<\/p>\n<p><!--more-->Adding <code>'admin' =&gt; true<\/code> is therefore just a named parameter in this case, but it comes with its advantages. Firstly, it can make code more succinct.<\/p>\n<pre><\/pre>\n<table width=\"760\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\">\n<tbody>\n<tr>\n<td>\/* Determine if a request came through admin routing *\/<br \/>\n\/\/ without:<br \/>\nif($this-&gt;params[&#8216;prefix&#8217;]==&#8217;admin&#8217;){}<br \/>\n\/\/ with:<br \/>\nif($this-&gt;params[&#8216;admin&#8217;]){}\/* Create a link that is reverse-routed to an admin prefixed route *\/<br \/>\n\/\/ without:<br \/>\n$html-&gt;link(&#8216;&#8230;&#8217;, array(&#8216;prefix&#8217;=&gt;&#8217;admin&#8217;,&#8217;controller&#8217;=&gt;&#8217;users&#8217;));<br \/>\n\/\/ with:<br \/>\n$html-&gt;link(&#8216;&#8230;&#8217;, array(&#8216;admin&#8217;=&gt;true,&#8217;controller&#8217;=&gt;&#8217;users&#8217;));<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Secondly, it provides backwards compatibility with the way admin routing worked in CakePHP 1.2 (the last line from the above example is how you would have made admin routing links in 1.2). Therefore, developers migrating from 1.2 to 1.3 can prevent having to change links throughout their application by keeping the <code>'admin' =&gt; true<\/code> flag in their routes (and adding the <code>'prefix' =&gt; 'admin'<\/code> one).<\/p>\n<p>Lastly, by setting a custom flag like this with a named parameter and using it in your application instead of referencing your route by an exact string means that you prevent yourself from ever having to change links if you change the prefix to something else (say from <code>admin<\/code> to <code>administrator<\/code> or <code>edit<\/code>)&#8230; although this is sort of a moot point, as you would need to rename all your <code>admin_*<\/code> controller actions and views. \ud83d\ude42<\/p>\n<p>From: <a href=\"http:\/\/stackoverflow.com\/questions\/3152252\/routing-admin-true-vs-prefix-admin-in-cakephp\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/3152252\/routing-admin-true-vs-prefix-admin-in-cakephp<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>By setting &#8216;prefix&#8217; =&gt; &#8216;admin&#8217; you are telling CakePHP that you want to use a prefix of admin for that route; basically meaning you want to use controller actions and views that have names prefixed with admin_. This part you &hellip; <a href=\"https:\/\/blog.ffteixeira.net\/?p=82\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=82"}],"version-history":[{"count":0,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts\/82\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}