laminas-api-tools / api-tools

Laminas API Tools module for Laminas
https://api-tools.getlaminas.org/documentation
BSD 3-Clause "New" or "Revised" License
37 stars 19 forks source link

Trying to get a factory/service inside resource #10

Open weierophinney opened 4 years ago

weierophinney commented 4 years ago

Tell me one thing if you can help me:

How can I get a Factory/Service inside a resource?


Originally posted by @luqezman at https://github.com/zfcampus/zf-apigility/issues/184

weierophinney commented 4 years ago

REST resource is created via container (service manager, if it is there defined) or directly: https://github.com/zfcampus/zf-rest/blob/master/src/Factory/RestControllerFactory.php#L117-L121 If you want inject something intro REST resource you can define your own factory for the resource and inject whatever you want.


Originally posted by @michalbundyra at https://github.com/zfcampus/zf-apigility/issues/184#issuecomment-269323820

weierophinney commented 4 years ago

Yes, but my ResourceFactory is not called or if I put a "exit" inside file, the php continues.

:'( I cloned apigility default project.


Originally posted by @luqezman at https://github.com/zfcampus/zf-apigility/issues/184#issuecomment-269326767

weierophinney commented 4 years ago

I just cloned the project, I created the service in apigility admin but ResourceFactory is never called or used.


Originally posted by @luqezman at https://github.com/zfcampus/zf-apigility/issues/184#issuecomment-269327409

weierophinney commented 4 years ago

Did you add to your configuration:

'service_manager' => [
     'factories' => [
        YourApi\V1\Rest\YourService\Resource::class => YourResourceFactory::class,
    ],
],

?

You can use also abstract factory if you want, or the same factory for multiple resources.


Originally posted by @michalbundyra at https://github.com/zfcampus/zf-apigility/issues/184#issuecomment-269329290

weierophinney commented 4 years ago

No I didn't. But I need to include this config inside module config or in global config?


Originally posted by @luqezman at https://github.com/zfcampus/zf-apigility/issues/184#issuecomment-269329421

weierophinney commented 4 years ago

Yeah Nice, it works like a charm. Big thanks dude <3


Originally posted by @luqezman at https://github.com/zfcampus/zf-apigility/issues/184#issuecomment-269329658

weierophinney commented 4 years ago

But I need to include this config inside module config or in global config?

Doesn't matter. If this is just for one module it should be in module.config.php. If this is a global configuration for your application (applied for multiple modules) than it can be in global config.


Originally posted by @michalbundyra at https://github.com/zfcampus/zf-apigility/issues/184#issuecomment-269329659

weierophinney commented 4 years ago

You can close this issue and a lot of people are searching for this in "google" or "stackoverflow".

Big thanks and HNY ;)


Originally posted by @luqezman at https://github.com/zfcampus/zf-apigility/issues/184#issuecomment-269330440