dannykopping/Slim — comment on commit 3fdc30d98d5038ec9520420fa6d81ef9de24b8a7
File:  line 0
By dannykopping:

Hey Josh

Hhhmm, i think our relative perspectives on decoupling are slightly
different.
My view of decoupling in this way is to completely decouple the Slim *
framework* from the callable, not the Slim *application instance*.

I was thinking... For all the points in the process where decoupling might
be problematic and potentially complicated for users, why don't we just use
(ironically) a callable to solve the issue...

Here's what I mean:

I, as a developer who wants to not allow my services layer to know anything
about Slim. My services must stay agnostic to Slim (or any other
HTTP-related piece) so that they can be multi-purposed... run from CLI,
HTTP or PHP-PHP includes.

If I could override Slim's route implementation, that might be provide me
with the flexibility I need:

$app->customRoute(function($callable, $params) {

$myCustomArgs = array("name" => "Danny");

// prepend any number of custom arguments acquired by whatever process
(deserialization of HTTP body, etc)
array_unshift($params, $myCustomArgs);

if(is_callable($callable))
$result = call_user_func_array($callable, array_values($params));

// no data back from callable, or callable is invalid
if(empty($result))
return;

echo json_encode($result);

});

Now I can control what data goes into the callable, and what to do with the
data that comes out.
My callable in my service layer will now receive all the data it needs to
effectively execute a task. It is now completely agnostic to the existence
of Slim Framework...

Thoughts?

*Danny Kopping* *Flash Platform Consultant*
Adobe Certified Expert (Flash, Flex, AIR)

http://dannykopping.com | (+27) 84 941 4422

On Fri, Jan 6, 2012 at 5:43 AM, Josh Lockhart <
reply@reply.github.com
> wrote:

> One last follow up for tonight. Here's all it would take to derive a
> `JsonResponse` class from `Slim_Http_Response`:
>
> class JsonResponse extends Slim_Http_Response {
> public function write( $json ) {
> $this['Content-Type'] = 'application/json';
> parent::write(json_encode($json));
> }
> }
>
> Used like this:
>
> $app->get('/foo.json', function () {
> return new JsonResponse(array(
> 'name' => 'Danny'
> ));
> });
>
> ---
> Reply to this email directly or view it on GitHub:
>
> https://github.com/dannykopping/Slim/commit/3fdc30d98d5038ec9520420fa6d81ef9de24b8a7#commitcomment-842595
>