Skip to content

Instantly share code, notes, and snippets.

@davedevelopment
Created January 17, 2013 08:58
Show Gist options
  • Save davedevelopment/4554678 to your computer and use it in GitHub Desktop.
Save davedevelopment/4554678 to your computer and use it in GitHub Desktop.
<?php
/**
* Ideas for parameter converters
*
* I've used the word argument here rather than convert or converter,
* because there may not be any converting as such. It may be that things are
* plucked out of the request etc
*
* The idea being that some of the controller arguments come from the url,
* some from the query or attributes and we're trying to make mapping routes to services easier and more explicit
* /
/**
* Extend request with setArgument(or some aptly named method), allowing total flexibility
*/
$app->post("/user/{user}/team", "user.controller.update_team:execute")
->convert('user', function($user) use ($app) { return $app['user.repository']->getUser($request->attributes->get('user_id')); })
->before(function(Request $request) use ($app) {
$request->setArgument('team', $app['team.repository']->getUser($request->attributes->get('team_id')));
});
/**
* Extend route with a flexible arguments(or some aptly named method),
* that can return a key value map of items to be used as arguments
*/
$app->post("/user/{user}/team", "user.controller.update_team:execute")
->convert('user', function($user) use ($app) { return $app['user.repository']->getUser($request->attributes->get('user_id')); })
->arguments(function(Request $request) use ($app) {
return array(
'team' => $app['team.repository']->getUser($request->attributes->get('team_id')),
);
});
/**
* Individual converters
*/
$app->post("/user/{user}/team", "user.controller.update_team:execute")
->convert('user', function($user) use ($app) { return $app['user.repository']->getUser($request->attributes->get('user_id')); })
->argument('team', function(Request $request) use ($app) {
return $app['team.repository']->getUser($request->attributes->get('team_id'));
});
/**
* Nothing special, we just manually wrap the controller for these circumstances
*/
$app->post("/user/{user}/team", function(User $user, Request $request, Application $app) {
$team = $app['team.repository']->getUser($request->attributes->get('team_id'));
return $app["user.controller.update_team"]->execute($user, $team);
})
->convert('user', function($user) use ($app) { return $app['user.repository']->getUser($request->attributes->get('user_id')); });
/**
* Individual converters, specific to fields.
*/
$app->post("/user/{user}/team", "user.controller.update_team:execute")
->convert('user', function($user) use ($app) { return $app['user.repository']->getUser($request->attributes->get('user_id')); })
->convertPost('team_id', "team", function($id) use ($app) {
return $app['team.repository']->getUser($id));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment