Inject Request int Service

2 years ago

iwory
Member | 147
+
0
-

Hello,
I would like to get instance of Nette\Application\Request to my own service.

But I don't know how can I inject it because everytime I get error message Parameter $name in Nette\Application\Request::__construct() has no class type hint or default value, so its value must be specified

Is there some way how to?

Thank you

2 years ago

CZechBoY
Member | 3360
+
0
-

Why do you need that? Pass value from AppRequest in presenter if you really need that.

2 years ago

David Matějka
Moderator | 5897
+
+2
-

Application request is not a service (there may be multiple app requests in a single http request), but you can inject Nette\Application\Application and get a current request from there

2 years ago

iwory
Member | 147
+
0
-

CZechBoY wrote:

Why do you need that? Pass value from AppRequest in presenter if you really need that.

I need it because I have service for prepare parameters and don't want to write it in presenter :)

2 years ago

iwory
Member | 147
+
0
-

David Matějka wrote:

Application request is not a service (there may be multiple app requests in a single http request), but you can inject Nette\Application\Application and get a current request from there

Yes, it works. Thx a lot

2 years ago

David Matějka
Moderator | 5897
+
0
-

I need it because I have service for prepare parameters and don't want to write it in presenter :)

well then may be better to follow @CZechBoY recommendation and pass the current Request object to the method you probably call from a presenter

2 years ago

iwory
Member | 147
+
0
-

David Matějka wrote:

I need it because I have service for prepare parameters and don't want to write it in presenter :)

well then may be better to follow @CZechBoY recommendation and pass the current Request object to the method you probably call from a presenter

Ok, I will follow recommendation from @CZechBoY and pass it from a presenter.

2 years ago

dkorpar
Member | 65
+
-1
-

You can inject Nette\Http\IRequest without any problems, I'm using it in severalservices, even in AOP…