Contextually aware controllers

Hi everyone, my CMS app is starting to get quite large. I have heaps
of modules (about 15) and some of these modules are re-used within
different contexts:

An example is that you can add a Page to the schools website, but you
can also add a page to a ‘Learning Cave’ which is our online learning
area for kids. While all the controller and view code is essentially
the same, we have it spread out (or copied) into two seperate
controllers because the layout for a Learning Cave is different to the
layout in the admin area where teachers add pages. Anyway, what I’m
wanting to be able to do is know the context in which my controller is
being called and use this to decide whether to run certain filters,
and also to decide the layout to use and other small things. This way
I can combine all the controller code into one controller but call it
from multiple places and have it respond differently depending on the
context. Here’s an example:

/admin/pages/new - shows the new page form using the layout for the
admin area (might also do some permission checking on the side that’s
only relevant to the admin area)

/learning_cave/pages/new - shows the same view and uses the same
controller but uses a different layout and possibly some other checks
that are only relevant for caves.

I’ve been attempting to do this with restful routing and
polymorphic_path (because I won’t know the context ahead of time) but
I’m having no luck. Does anyone know if there is already anything out
there that would assist with this approach? I’m trying to package all
my modules as rails engines and something like this would help