title: Front-Controller Framework Architecture and Services
body: <p>I am trying to use my service layer in my own front-controller style framework and I've always been taught to decouple the service layer from the controller layer.</p>
  
  <p>This is causing me issues as I need to access important front-controller methods from my services, so I'm jumping through hoops to try and make this happen.</p>
  
  <p>I noticed when looking at Coldbox however that the actual framework regularly passes the Coldbox controller through to its services.</p>
  
  <p>Can and should this really be done?</p>
  
  <p>The problem I have specifically that my controller/handler calls a service which gets a load of widgets for a page request, it then loops over each one and renders each widget, but each widget is itself a controller/handler and the controller/handler needs the front-controller.</p>
  
  <p>How can this be done without passing in the front-controller or is my architecture all wrong?</p>
  
  <p>CURRENT SERVICE CODE (shortened)</p>
  
  <pre><code>&lt;cffunction name="renderWidgets" access="public" output="false" returntype="string" hint="I return an the prequested webpage URL."&gt;
  &lt;cfargument name="source" type="string" default="" /&gt;
  &lt;cfargument name="templateId" type="string" default="" /&gt;
  &lt;cfargument name="webPageId" type="string" default="" /&gt;
  &lt;!--- set original template HTML ---&gt;
  &lt;cfset stuReturn.renderedHTML = arguments.source /&gt;
  &lt;!--- get assigned widgets and properties ---&gt;
  &lt;cfset stuReturn.qryTemplateObjects = findTemplateObjectAssignments({templateId=arguments.templateID}) /&gt;
  &lt;!--- if found ---&gt;
  &lt;cfif stuReturn.qryTemplateObjects.recordcount&gt;
      &lt;!--- loop over assigned widgets ---&gt;
      &lt;cfloop query="stuReturn.qryTemplateObjects"&gt;
          &lt;!--- create struct  ---&gt;
          &lt;cfset stuReturn.stuTemplateObjectAssignment = queryrowtostruct(stuReturn.qryTemplateObjects, stuReturn.qryTemplateObjects.currentrow) /&gt;
          &lt;!--- get the widget object ---&gt;
          &lt;cfset stuReturn.renderedHTML = stuReturn.renderedHTML &amp; getFrontController().getWidget(stuReturn.stuTemplateObjectAssignment.objectId).render() /&gt;&lt;!--- this is the problem, no access to getFrontController() ---&gt;
      &lt;/cfloop&gt;
  &lt;/cfif&gt;
  &lt;!--- return rendered HTML ---&gt;
  &lt;cfreturn stuReturn.renderedHTML /&gt;
  </code></pre>
  
  <p>   </p>