# External callback function for routes

**URL:** <https://discourse.slimframework.com/t/external-callback-function-for-routes/1287>\
**Category:** Questions\
**Created:** [March 17, 2017, 1:59pm UTC](https://discourse.slimframework.com/t/external-callback-function-for-routes/1287 "2017-03-17T13:59:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Senshi](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@Senshi](https://discourse.slimframework.com/u/Senshi)\
**Post date:** [March 17, 2017, 1:59pm UTC](https://discourse.slimframework.com/t/external-callback-function-for-routes/1287/1 "2017-03-17T13:59:15Z")

</div>

I’m pretty sure this is just my lack of understanding of how to use PHP callables properly, but I hope you guys can help me anyway:  
I already have a lot of routes that work just fine:  
$app-\>get(’/test’, function (Request $request, Response $response) {  
lots of stuff});

However, the function body of many routes becomes very unwieldy, so I’d like to refactor everything, so the actual code is inside specific functions, and only a very short route declaration is left over that I can manage in a more central place. This gives me a bunch of benefits (easier to read, swagger&phpdoc for functions, useful function names in outline instead of \_\_anonymous etc.).  
So I simply moved the function on its own, and gave it a name:  
function get\_test( Request $request, Response $response) {  
lots of stuff});  
and then defined the route as  
`$app->get('/test', get_test($request, $response));`

Which doesn’t work, giving me a “$request is undefined”) error message. What does work:  
`$app->get('/test', function($request, $response) {return get_test($request, $response);});`  
Which however feels unnecessarily bulky.

Surely there’s an easy and simple way to do what I am looking for?

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [March 17, 2017, 2:29pm UTC](https://discourse.slimframework.com/t/external-callback-function-for-routes/1287/2 "2017-03-17T14:29:53Z")

</div>

I believe what you are looking for is the [Container Resolution](https://www.slimframework.com/docs/objects/router.html#container-resolution) section of the docs. Instead of using a function you can use a Controller class. You can see an example of this in the [slim-bookshelf](https://github.com/akrabat/slim-bookshelf) example app, [routes.php](https://github.com/akrabat/slim-bookshelf/blob/master/app/src/routes.php).

---

<div class="post-metadata">

**Author:** ![Senshi](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@Senshi](https://discourse.slimframework.com/u/Senshi)\
**Post date:** [March 17, 2017, 2:34pm UTC](https://discourse.slimframework.com/t/external-callback-function-for-routes/1287/3 "2017-03-17T14:34:41Z")

</div>

That is exactly the kind of easy solution I was looking for 🙂 . In my case, it would be as simple as:  
`$app->get('/test', 'get_test');`

I failed because the correct solution is even shorter&smarter than I expected. I love Slim more and more \<3 .
