# Slim with Controller implementation

**URL:** <https://discourse.slimframework.com/t/slim-with-controller-implementation/1161>\
**Category:** Questions\
**Created:** [February 17, 2017, 6:33am UTC](https://discourse.slimframework.com/t/slim-with-controller-implementation/1161 "2017-02-17T06:33:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anishdoot](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anishdoot](https://discourse.slimframework.com/u/anishdoot)\
**Post date:** [February 17, 2017, 6:33am UTC](https://discourse.slimframework.com/t/slim-with-controller-implementation/1161/1 "2017-02-17T06:33:17Z")

</div>

Hi,

I just develop an API using slim framework. Implementing controller functionality i got error.

This is the way of am implementing

I just create a folder controllers in src folder

Then add new file HomeController.php with

```
      namespace src\Controllers;

        class HomeController
        {
        	public function getuser($request,$response,$args)
        	{
        	    var_dump($request);
        	}
        }

```

then add dependencies.php file

```
$container['HomeController'] = function ($c)
{
	return new src\Controllers\HomeController;
};

```

Then add router file

$app-\>post(’/getuser’,‘HomeController:getuser’);

I got this error

**Fatal error: Class ‘App\Controllers\HomeController’ not found in /var/www/html/doothdvlp/api/v1/src/dependencies.php on line 39**

Please help me

---

<div class="post-metadata">

**Author:** ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)\
**Post date:** [February 17, 2017, 8:07am UTC](https://discourse.slimframework.com/t/slim-with-controller-implementation/1161/2 "2017-02-17T08:07:48Z")

</div>

Change

```auto
namespace src\Controllers;

```

to:

```auto
namespace App\Controllers;

```

---

<div class="post-metadata">

**Author:** ![anishdoot](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anishdoot](https://discourse.slimframework.com/u/anishdoot)\
**Post date:** [February 17, 2017, 9:13am UTC](https://discourse.slimframework.com/t/slim-with-controller-implementation/1161/3 "2017-02-17T09:13:56Z")

</div>

I change the namespace to **namespace App\Controllers;**. Still i got the error

---

<div class="post-metadata">

**Author:** ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)\
**Post date:** [February 17, 2017, 9:28am UTC](https://discourse.slimframework.com/t/slim-with-controller-implementation/1161/4 "2017-02-17T09:28:09Z")

</div>

You need to read up on namespaces and how they relate to PSR-4.

Look at how the route, DI factory (in dependencies.php) and the controller interact in [https://github.com/akrabat/slim-bookshelf](https://github.com/akrabat/slim-bookshelf).

---

<div class="post-metadata">

**Author:** ![victor](https://avatars.discourse-cdn.com/v4/letter/v/3e96dc/32.png) [@victor](https://discourse.slimframework.com/u/victor)\
**Post date:** [February 23, 2017, 2:59pm UTC](https://discourse.slimframework.com/t/slim-with-controller-implementation/1161/5 "2017-02-23T14:59:43Z")

</div>

Look composer.json file autoload key:

```
"autoload": {
    "psr-4": {
        "App\\": "app/src"
    }
},

```

psr-4 indicates translation between namespaces and folders. In this case:

```
namespace App\Controllers

```

look inside app/src/controllers folder

Regards,
