# How to use services in controllers

**URL:** <https://discourse.cakephp.org/t/how-to-use-services-in-controllers/12656>\
**Category:** Uncategorized\
**Created:** [July 25, 2025, 5:04am UTC](https://discourse.cakephp.org/t/how-to-use-services-in-controllers/12656 "2025-07-25T05:04:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![VishalPanchal45](https://avatars.discourse-cdn.com/v4/letter/v/b3f665/32.png) [@VishalPanchal45](https://discourse.cakephp.org/u/VishalPanchal45)\
**Post date:** [July 25, 2025, 5:04am UTC](https://discourse.cakephp.org/t/how-to-use-services-in-controllers/12656/1 "2025-07-25T05:04:04Z")

</div>

Hello,

I upgraded cakephp from 4.5.9 to 5.1.4. I was using serviceawaretrait to load services at base controller. But now I found that CakePHP 5.1.4 standard way is DI. I tried it but getting errors when I try to use services in controller. For the context, I have many controllers and services so I need help to guide me how can I fix the errors and able to use existing services in existing controllers with minimal code changes.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [July 25, 2025, 5:11am UTC](https://discourse.cakephp.org/t/how-to-use-services-in-controllers/12656/2 "2025-07-25T05:11:43Z")

</div>

See [Dependency Injection - 5.x](https://book.cakephp.org/5/en/development/dependency-injection.html#controller-example)

---

<div class="post-metadata">

**Author:** ![VishalPanchal45](https://avatars.discourse-cdn.com/v4/letter/v/b3f665/32.png) [@VishalPanchal45](https://discourse.cakephp.org/u/VishalPanchal45)\
**Post date:** [July 25, 2025, 5:29am UTC](https://discourse.cakephp.org/t/how-to-use-services-in-controllers/12656/3 "2025-07-25T05:29:53Z")

</div>

Yes, I see that. Can’t we use constructor based injection instead of method injection? Even Method injection not working.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [July 25, 2025, 5:32am UTC](https://discourse.cakephp.org/t/how-to-use-services-in-controllers/12656/4 "2025-07-25T05:32:20Z")

</div>

constructor based injection isn’t preferred/working here since the controller isn’t created by the container.

And what exactly do you mean by “its not working”. Please give more context. Do you get an error. Do you call the right URL. what code example can you show. I can’t help you with “its not working”

---

<div class="post-metadata">

**Author:** ![VishalPanchal45](https://avatars.discourse-cdn.com/v4/letter/v/b3f665/32.png) [@VishalPanchal45](https://discourse.cakephp.org/u/VishalPanchal45)\
**Post date:** [July 25, 2025, 5:52am UTC](https://discourse.cakephp.org/t/how-to-use-services-in-controllers/12656/5 "2025-07-25T05:52:47Z")

</div>

Ok let say, I have below files.

- TestController: addTest()
- TestService: ValidateTestExistance()

Now, Application.php has below code

```auto
public function services(ContainerInterface $container): void
    {
        $container
            ->add(TestService::class);
    }

```

TestsController:

```auto
public function addTest(TestService $testService){
   $validate = $testService->ValidateTestExistance();
...............................
}

```

So I am getting error like function ValidateTestExistance called on null. It means that I am not getting service object here.

JFI, I have existing code with ServiceAwareTraits and ServiceRegistry…

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [July 25, 2025, 5:58am UTC](https://discourse.cakephp.org/t/how-to-use-services-in-controllers/12656/6 "2025-07-25T05:58:59Z")

</div>

No idea what you mean by `ServiceAwareTraits` and `ServiceRegistry`, those are not default CakePHP

But before doing that, just try `var_dump($testService);` and see if you get an instance.

Also make sure you have the correct use statements at the top of your controller to correctly import your TestService class

---

<div class="post-metadata">

**Author:** ![VishalPanchal45](https://avatars.discourse-cdn.com/v4/letter/v/b3f665/32.png) [@VishalPanchal45](https://discourse.cakephp.org/u/VishalPanchal45)\
**Post date:** [July 25, 2025, 6:04am UTC](https://discourse.cakephp.org/t/how-to-use-services-in-controllers/12656/7 "2025-07-25T06:04:37Z")

</div>

Ok, Anyway please can you tell me that do we have any other approach than to use services as method injection in controller class?

As, my current project has lots of methods and controllers. Can we do something like create helper function which loads the service from container and then inject in controller’s constructor?

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [July 25, 2025, 7:44am UTC](https://discourse.cakephp.org/t/how-to-use-services-in-controllers/12656/8 "2025-07-25T07:44:21Z")

</div>

From what you described it should work. All you need to do is add the service to the container in your `src/Application.php`

```php
// At the Top of your class
use App\Services\TestService;

public function services(ContainerInterface $container): void
{
    $container->add(TestService::class);
}

```

and then you can do in any controller

```php
// At the Top of your class
use App\Services\TestService;

public function add(TestService $testService) {
   var_dump('I am here');
   var_dump($testService);
}

```

I’d make sure that you actually call the correct controller action by checking, that you get the `I am here` output. Maybe you think you are calling the correct URL even though you are going somewhere different.
