Controllers: same class name with different packages

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Controllers: same class name with different packages

Matthias Hryniszak-2
Hi there,

I'm wondering how to achieve such a thing: I have 2 packages containing controllers. Let's say app.pkg1.controllers and com.example.app.pkg2.controllers. Let's also say there's a logic in naming controller classes in both packages HomeController. So we have com.example.app.pkg1.controllers.HomeController and app.pkg1.controllers.HomeController.

Next I need to provide proper URL mappings so that those 2 controllers can be called. Let's say that the mapping result in following URLs:

/home1 -> com.example.app.pkg1.controllers.HomeController
/home2 -> com.example.app.pkg2.controllers.HomeController

I've tried specifying the the following in UrlMappings.groovy in my grails-app/conf folder:

- full class name as string
- the class itself

However I can't force it to work.

Can you help me out?

Best regards,
Matthias.
Reply | Threaded
Open this post in threaded view
|

Re: Controllers: same class name with different packages

Ian Roberts
On 01/04/2011 14:44, Matthias Hryniszak wrote:
> However I can't force it to work.

It's not possible, at least certainly not in Grails 1.3.x.  Grails
conventions require that all controller class names (ignoring the
package prefix) are distinct.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Controllers: same class name with different packages

Michael Bennett
yes I found this out the hard way after installing spring security ui and creating the UserController for admin of my users... much head scratching ensued. even though you see two links on the available controllers list it only goes to one.

On 1 April 2011 14:54, Ian Roberts <[hidden email]> wrote:
On 01/04/2011 14:44, Matthias Hryniszak wrote:
> However I can't force it to work.

It's not possible, at least certainly not in Grails 1.3.x.  Grails
conventions require that all controller class names (ignoring the
package prefix) are distinct.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Controllers: same class name with different packages

Ian Roberts
On 01/04/2011 15:24, Michael Bennett wrote:
> yes I found this out the hard way after installing spring security ui
> and creating the UserController for admin of my users... much head
> scratching ensued. even though you see two links on the available
> controllers list it only goes to one.

Of course the other thing to remember is that the /controller/action/id
URL mapping is only a convention.  There's nothing to stop you having a
URL mapping like:

"/$section/$ctrl/$action?/$id?" {
  controller = { params.section +
GrailsNameUtils.getClassNameRepresentation(params.ctrl) }
}

Which would map /foo/home/show to FooHomeController, /bar/home/show to
BarHomeController, etc.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Controllers: same class name with different packages

Michael Bennett
Hi Ian,

Thats a really nice bit of code! That would sort out some issues for sure, just need to remember its in place and have some error handling to cope with erroneous or malicious url manipulation.

thanks!

Michael

On 1 April 2011 15:40, Ian Roberts <[hidden email]> wrote:
On 01/04/2011 15:24, Michael Bennett wrote:
> yes I found this out the hard way after installing spring security ui
> and creating the UserController for admin of my users... much head
> scratching ensued. even though you see two links on the available
> controllers list it only goes to one.

Of course the other thing to remember is that the /controller/action/id
URL mapping is only a convention.  There's nothing to stop you having a
URL mapping like:

"/$section/$ctrl/$action?/$id?" {
 controller = { params.section +
GrailsNameUtils.getClassNameRepresentation(params.ctrl) }
}

Which would map /foo/home/show to FooHomeController, /bar/home/show to
BarHomeController, etc.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Controllers: same class name with different packages

Ian Roberts
On 01/04/2011 15:51, Michael Bennett wrote:
> Hi Ian,
>
> Thats a really nice bit of code! That would sort out some issues for
> sure, just need to remember its in place and have some error handling to
> cope with erroneous or malicious url manipulation.

And note that it will probably only do the "forward" mapping, so isn't
particularly useful if you're using scaffolding.  I.e. <g:link
controller="fooHome"> won't necessarily map back the other way, you may
find you have to use <g:link params="[section:'foo',ctrl:'home']"> instead.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Controllers: same class name with different packages

Matthias Hryniszak-2
In reply to this post by Ian Roberts
Sure... but this damn thing will map the FooHomeController in pkg1 and in pkg2 to the same name. The problem is that I have same controller class names in different plugins and a glitch occurs.

All and all it's just irrational not to allow for controller classes to be specified directly and only relaying on convention.

Luckily I've found a damn universal solution.

In bootstrap add this code:

grailsApplication.getArtefactInfo("Controller").grailsClasses.each {
  it.uri2closureMap.put('/' + it.fullName, 'index')
  it.uri2closureMap.put('/' + it.fullName + '/', "index")
  it.uri2closureMap.put('/' + it.fullName + '/index', "index")
  it.uri2closureMap.put('/' + it.fullName + '/index/**', "index")
  it.configureURIsForCurrentState()
}

And with this you can use full controller name (with package) to do the mapping :)

2011/4/1 Ian Roberts <[hidden email]>
getClassNameRepresentation

Reply | Threaded
Open this post in threaded view
|

Re: Controllers: same class name with different packages

Matthias Hryniszak-2
http://padcom13.blogspot.com/2011/04/grails-gotcha-controllers-with-same.html

2011/4/1 Matthias Hryniszak <[hidden email]>
Sure... but this damn thing will map the FooHomeController in pkg1 and in pkg2 to the same name. The problem is that I have same controller class names in different plugins and a glitch occurs.

All and all it's just irrational not to allow for controller classes to be specified directly and only relaying on convention.

Luckily I've found a damn universal solution.

In bootstrap add this code:

grailsApplication.getArtefactInfo("Controller").grailsClasses.each {
  it.uri2closureMap.put('/' + it.fullName, 'index')
  it.uri2closureMap.put('/' + it.fullName + '/', "index")
  it.uri2closureMap.put('/' + it.fullName + '/index', "index")
  it.uri2closureMap.put('/' + it.fullName + '/index/**', "index")
  it.configureURIsForCurrentState()
}

And with this you can use full controller name (with package) to do the mapping :)

2011/4/1 Ian Roberts <[hidden email]>
getClassNameRepresentation