{"id":8061,"date":"2012-09-24T12:11:11","date_gmt":"2012-09-24T06:41:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8061"},"modified":"2016-12-19T14:50:27","modified_gmt":"2016-12-19T09:20:27","slug":"grails-2-2-0-namespacing-to-resolve-between-controllers-of-same-name","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-2-2-0-namespacing-to-resolve-between-controllers-of-same-name\/","title":{"rendered":"Grails 2.2.0 : Namespacing to resolve between controllers of same name"},"content":{"rendered":"<p>Grails 2.2.0.RC1 was released last week and a new feature to make an entry into the version of the framework is the support for namespaces for controllers and services so that there is no naming conflicts while adding artefacts of the same name from different plugins or within the application.<\/p>\n<p>I created a sample app and installed the grails console plugin. This brought a ConsoleController to the application.<\/p>\n<p>[java]<br \/>\ngrails install-plugin console<br \/>\n[\/java]<\/p>\n<p>Next, I added a controller with the same name, ConsoleController.groovy in the application.<\/p>\n<p>[java]<br \/>\ngrails create-controller console<br \/>\n[\/java]<\/p>\n<p>To resolve the two at a mappings level, we need to add an entry in UrlMappings.groovy so that the two URLs are different.<\/p>\n<p>[java]<br \/>\n\/\/UrlMappings.groovy<br \/>\nclass UrlMappings {<\/p>\n<p>        static mappings = {<br \/>\n                &amp;quot;\/gconsole&amp;quot;{ \/\/Resolve gconsole to ConsoleController in the plugin<br \/>\n                        controller = &amp;quot;console&amp;quot;<br \/>\n                        plugin = &amp;quot;console&amp;quot;<br \/>\n                }<br \/>\n                &amp;quot;\/$controller\/$action?\/$id?&amp;quot;{<br \/>\n                        constraints {<br \/>\n                                \/\/ apply constraints here<br \/>\n                        }<br \/>\n                }<\/p>\n<p>                &amp;quot;\/&amp;quot;(view:&amp;quot;\/index&amp;quot;)<br \/>\n                &amp;quot;500&amp;quot;(view:&#8217;\/error&#8217;)<br \/>\n        }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>Now, if I want to hit the console controller from the application, I can navigate to: <strong><em>http:\/\/&lt;app_base_url&gt;\/console<\/em><\/strong><\/p>\n<p>And to navigate to console controller from the plugin, I can navigate to:<strong> <em>http:\/\/<\/em><em>&lt;app_base_url&gt;<\/em><em>\/gconsole<\/em><\/strong><\/p>\n<p>To use this in the <em><strong>&lt;g:link&gt;<\/strong><\/em> tag, all we need to do is provide an extra attribute <em><strong>plugin<\/strong><\/em> and grails will create the URL accordingly.<\/p>\n<p>This means that we can have choose better namings for our controllers without losing control(pun unintended) over their names.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grails 2.2.0.RC1 was released last week and a new feature to make an entry into the version of the framework is the support for namespaces for controllers and services so that there is no naming conflicts while adding artefacts of the same name from different plugins or within the application. I created a sample app [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[1031,1032,1030],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8061"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8061"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8061\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}