Shortcut and url scope

hi all

I’m new.
I’m trying to rewrite my cf apps for Lucee.

in cf, generally, I create a “shortcut” for invoke my controllers.
something like this, in Application.cfc:

  public Struct function ctrl(required String name) {
      return application.controllers['main'][arguments.name];
  }

in this way, I can write in my cfml page, without use any scope:

ctrl('User')

this works well in cf.
in Lucee it get an error: “function not found”.

I have to add the function to the “url” scope, but it doesn’t seem like a good solution.

any idea?

many thanks :upside_down_face:

Hi Roberto and welcome.

Could you provide a bit more detail about how your application is structured? Are you using a standard framework such as FW/1 or ColdBox, or is it your own custom MVC set up?

Also, which version of Lucee?

In my opinion it is not a good idea, to try avoiding the scope.
For an external developer it can be hard to debug a function, when he dont know where the code is comming from.
I would create a cfc (maybe with a mapping) and call the function of it.
Eg. [mapping.]Common.ctrl()

Hi Julian
thanks for the welcome :slight_smile:

Lucee version: 5.3.3.60-RC

I load services by ColdSpring.
I dont’ use any mvc framework.

i load controllers in Application.cfc, like this:

   public Boolean function onApplicationStart(){
       application.controllers = startController();
   }
 
   private Struct function startController(){

       var controllers = {};

       controllers['main'].status   = new controller.main.StatusController();
       controllers['main'].log      = new controller.main.LogController();
       controllers['ajax'].log      = new controller.manager.LogAjaxController();

       return controllers;
   }

to call a function of a controller in my .cfm page, I should write:

application.controllers['main'].status.func()

to long and and boring :stuck_out_tongue:

If you use onRequest in your Application.cfc to include the requested cfm it should make your ctrl() function (any all public functions in Application.cfc) avaliable to the cfm files because they are part of the variables scope for that chain of execution.

As Pete says, you need to do more than just create a function in Application.cfc for it to be available in your .cfm files.

This applies to ACF as well as Lucee. Using the latest ACF, if I create a simple method test() in Application.cfc and try and call it from a .cfm script I get “Variable TEST is undefined”. There must be something else going on in your app for it to work in ACF.

Why the same code apparently isn’t working in Lucee is the question, but perhaps Pete’s suggestion will help.

1 Like

thanks @ pfreitag
I’ll do anything you say

thanks :slight_smile:

1 Like

Hey Roberto,

To build on the feedback from @pfreitag

in this way, I can write in my cfml page, without use any scope:

By not supplying a scope to lucee it’s going to take a lot more effort for it to resolve your statement which is fine in smaller applications but difficult at scale:

ctrl('User')

vs.

request.app.ctrl('User')

Better to use an explicit scope like request because it’s easy for lucee to recognize. Which could be initialized with a ‘controller’ object on it that you can then call ctrl() on.

1 Like

@jasperboyd,
thanks for explanation :slight_smile:

1 Like