# How to use Restful Controller on Slim 3

**URL:** <https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419>\
**Category:** Questions\
**Created:** [June 21, 2016, 9:37am UTC](https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419 "2016-06-21T09:37:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hoquangthaiholy](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/hoquangthaiholy/32/79_2.png) [@hoquangthaiholy](https://discourse.slimframework.com/u/hoquangthaiholy)\
**Post date:** [June 21, 2016, 9:37am UTC](https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419/1 "2016-06-21T09:37:46Z")

</div>

I registered route with Restful Controller like this:

`$app->any('/user', 'UserController');`

This is my codes for UserController

```
namespace App;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class UserController
{
    function __invoke(){
    }

    function list(ServerRequestInterface $request, ResponseInterface $response){
        return $response;
    }
}

```

I tried to access [http://localhost/demo/user/list](http://localhost/demo/user/list). But it isn’t working and return status code 404. There’re no guide for Restul Controller in document [here](http://www.slimframework.com/docs/objects/router.html). Help me, please.

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [June 21, 2016, 10:04am UTC](https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419/2 "2016-06-21T10:04:22Z")

</div>

I don’t know the answer to your problem off the top of my head, but saying “it’s not working” is not very helpful. Please be specific in what type of error/issue you’re getting and it will be much easier for us to help you.

---

<div class="post-metadata">

**Author:** ![hoquangthaiholy](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/hoquangthaiholy/32/79_2.png) [@hoquangthaiholy](https://discourse.slimframework.com/u/hoquangthaiholy)\
**Post date:** [June 21, 2016, 10:09am UTC](https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419/3 "2016-06-21T10:09:27Z")

</div>

Sorry, It’s return status code 404.

---

<div class="post-metadata">

**Author:** ![hoquangthaiholy](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/hoquangthaiholy/32/79_2.png) [@hoquangthaiholy](https://discourse.slimframework.com/u/hoquangthaiholy)\
**Post date:** [June 21, 2016, 10:20am UTC](https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419/4 "2016-06-21T10:20:30Z")

</div>

Ok, sorry. I understand right now. I should handle $request & $response in \_\_invoke. Thanks.

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [June 21, 2016, 10:29am UTC](https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419/5 "2016-06-21T10:29:09Z")

</div>

Glad you were able to solve your problem. I would suggest however that you change your route definition to

```
$app->any('/user', 'UserController:list')->setName('list');

```

since this will invoke the list() method directly and not rely on the magic \_\_invoke() method. Doing it this way also allows you to cleanly separate multiple route functions in a single class.

Note: the setName() call is not required but it’s considered good practice to do this as it allows you to later on query the route name should you need to make decision based upon which route was called.

---

<div class="post-metadata">

**Author:** ![hoquangthaiholy](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/hoquangthaiholy/32/79_2.png) [@hoquangthaiholy](https://discourse.slimframework.com/u/hoquangthaiholy)\
**Post date:** [June 21, 2016, 10:38am UTC](https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419/6 "2016-06-21T10:38:21Z")

</div>

Thanks for your suggest. I want to make a parent class with \_\_invoke() to handle $request & $response which can map method to route automatically from child class.

And then, I can simple register child class UserController for example

`$app->any('/user', 'UserController')`

with

getList =\> GET /user/list  
postList =\> POST /user/list  
getShowProfile =\> GET /user/show-profile  
…

It should be good or bad idea?

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [June 21, 2016, 10:44am UTC](https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419/7 "2016-06-21T10:44:23Z")

</div>

Quite honestly, I don’t know. My first thought is that you’re overcomplicating things, but if this approach works for you and you’re happy with it, then by all means continue using it.

---

<div class="post-metadata">

**Author:** ![hoquangthaiholy](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/hoquangthaiholy/32/79_2.png) [@hoquangthaiholy](https://discourse.slimframework.com/u/hoquangthaiholy)\
**Post date:** [June 21, 2016, 10:47am UTC](https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419/8 "2016-06-21T10:47:13Z")

</div>

OK, maybe you’re right. I should take a look for another way.

Thanks so much.

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [June 21, 2016, 11:20am UTC](https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419/9 "2016-06-21T11:20:52Z")

</div>

Here’s an excerpt from my routes.php.

```
$app->get ('/faq', 'App\Controller\UserController:faq')->setName ('guest_faq');
$app->get ('/login', 'App\Controller\UserController:login')->setName ('guest_login');
$app->get ('/logout', 'App\Controller\UserController:logout')->setName ('guest_logout');

```

In my opinion (and I may not be 100% correct here) this is simple, clear and readable.

Please don’t misunderstand: I’m not trying to tell you how to write your code, I’m just trying to give the best advice I know for Slim.

---

<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:** [June 26, 2016, 2:16pm UTC](https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419/10 "2016-06-26T14:16:35Z")

</div>

> [@hoquangthaiholy](#):
>
> I want to make a parent class with \_\_invoke() to handle $request & $response which can map method to route automatically from child class.
> 
> And then, I can simple register child class UserController for example
> 
> $app-\>any(‘/user’, ‘UserController’)
> 
> with
> 
> getList =\> GET /user/listpostList =\> POST /user/listgetShowProfile =\> GET /user/show-profile…
> 
> It should be good or bad idea?

I’ve seen that pattern used successfully before. No reason not to do it if you want to have a single mapping in your routes list.

---

<div class="post-metadata">

**Author:** ![LosLobos](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/loslobos/32/142_2.png) [@LosLobos](https://discourse.slimframework.com/u/LosLobos)\
**Post date:** [June 28, 2016, 7:19pm UTC](https://discourse.slimframework.com/t/how-to-use-restful-controller-on-slim-3/419/11 "2016-06-28T19:19:35Z")

</div>

I don’t know if it’ll be usefull for you, but if you want to know how to pass the Request, Response Objects to your own Controller Class instead of the Method, this post [Here](http://discourse.slimframework.com/t/slim-3-container-resolution/200) will be usefull to you.

Its tricky and not out of the box but in my case I found it usefull and as I can see you are looking for more customization and control.
