question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pass Request object to Controller constructor

See original GitHub issue

Sorting

  • I’m submitting a …

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn’t already been submit

Expected Behavior

It would be useful to have the Request object in the controller constructor, not only on the handler method. It’s similar to what was asked on #7. The issue was solved in #25 for DI, but not for non-DI controllers. I don’t know if there is a reason for not doing it.

// In the generated routes.ts file
validatedArgs = getValidatedArgs(args, request, response);
const controller = new MyController(request)

const promise = controller.myHandlerMethod.apply(
  controller,
  validatedArgs as any
)
promiseHandler(controller, promise, response, undefined, next)

Current Behavior

The Controller constructor is called with no arguments.

Possible Solution

It seems to me that a simple change in the handlebars templates would do. For example, in the express.hb, this is the current code:

{{#if ../../iocModule}}
  const container: IocContainer = typeof iocContainer === 'function' ? (iocContainer as IocContainerFactory)(request) : iocContainer;

  const controller: any = await container.get<{{../name}}>({{../name}});
  if (typeof controller['setStatus'] === 'function') {
    controller.setStatus(undefined);
  }
{{else}}
  const controller = new {{../name}}();
{{/if}}

Changing the else part to:

{{else}}
  const controller = new {{../name}}(request);
{{/if}}

Seems to do the job. It’s the same request that is being passed to IoCContainerFactory.

Breaking change?

Currently the constructor is called without arguments, I’m not sure adding an argument is breaking. Maybe for people that are using default parameters in the controller constructor? Like:

class MyController {
  constructor(myArg = 'some default arg') {}
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
Kampfmoehrecommented, Jun 23, 2022

A non breaking way could be to use a property of the controller. Maybe add a request property to the controller class and then inject that property from the route template after initialization. This way it is not interfering with other constructor arguments, independent if using DI or not and also spares to set a request like object in every unit test even if the method to test doesn’t need it (in contrary to constructor injection). If users want more control or do additional stuff on the request there could be an overridable setRequest or setContext method that the route template calls. For exmplae

export class Controller {
  public request?: Request;

  public setContext(request: Request) {
    this.request = request;
  }
}

and

  let controller: any;
{{#if ../../iocModule}}
  controller = await container.get<{{../name}}>({{../name}});
  if (typeof controller['setStatus'] === 'function') {
    controller.setStatus(undefined);
  }
{{else}}
  controller = new {{../name}}();
{{/if}}
  controller.setContext(request);
0reactions
github-actions[bot]commented, May 6, 2022

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass Request to Controller constructor in Symfony2?
Just pass RequestStack $requestStack as parameter in the constructor. namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\ ...
Read more >
Injecting Request in the controller's constructor with Laravel 8 ?
So is it safe to inject Request directly in the controller's constructor with Laravel 8 ? Are there any side effects I should...
Read more >
Dependency injection into controllers in ASP.NET Core
ASP.NET Core MVC controllers request dependencies explicitly via constructors. ASP.NET Core has built-in support for dependency injection (DI).
Read more >
Dependency Injection and Controllers
ASP.NET Core MVC controllers should request their dependencies explicitly via their constructors. In some instances, individual controller actions may require a ...
Read more >
Controller (Symfony Docs)
A controller is a PHP function you create that reads information from the Request object and creates and returns a Response object. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found