[Coldbox 7.2.1] Is it Possible to Get the RequestContext In a Model?

I am building a User entity model so it can send various emails. I would like to be able to use the buildLink() method inside the RequestContext to aid in building links inside the emails. Is it possible to get an instance of coldbox.system.web.context.RequestContext inside of my module? When I attempt to inject an instance of it with wirebox, I get the following error message:

“The CONTROLLER parameter to the init function is required but was not passed in.”

Here’s a simplified example of what I am trying to accomplish:

// User Quick Entity

// send a password reset email to the user
void function emailPasswordReset() {

	var email = _wirebox.getInstance( "mailService@cbmailservices" ).newMail(
		to = getEmailAddress(),
		from = emailSettings.from,
		subject = settings.password.reset.emailSubject,
		type = "html",
		body = renderer.view( 
			view = "emails/passwordResetRequest",
			args = {
				emailSubject = settings.password.reset.emailSubject,
				preHeader = settings.password.reset.emailPreHeader,
				user = this

	var mailResult = email.send( email );


// the email view will request the password reset link
string function renderPasswordResetLink() {
	return _wirebox
		.getInstance( "coldbox.system.web.context.RequestContext" )
		.buildLink( "login.update", {
			emailAddress: getEmailAddress(),
			key: getPasswordResetKey()
		} );

It would appear the RequestContext object lives in the request scope, so you can access buildLink() in a model like this:

request.cb_requestcontext.buildLink( 'your.cool.link' );

Now this feels like a code smell to me because it breaks encapsulation. However, that being said, the request scope is global for a reason, so I could also see reasons why it might be okay to break encapsulation once in awhile.

I wonder if accessing the RequestContext directly in a model would have side effects when writing tests? I will have to experiment with it.

There’s a bit quicker way to do it with wirebox:

property name="requestService" inject="coldbox:RequestService";

Then in your model you can just call variables.requestService.getContext()

You definitely don’t want to inject the context in to a model, but you can retrieve it through the service - which is a singleton


You saved the day! This solution is perfect!