Skip to content

Instantly share code, notes, and snippets.

@clops
Created August 19, 2013 13:15
Show Gist options
  • Save clops/6269005 to your computer and use it in GitHub Desktop.
Save clops/6269005 to your computer and use it in GitHub Desktop.
This is how to configure a Service in Symfony 2 with dynamic parameters. No magic, but still awesome.
<?
//..
//the following will return an Instance of InstagramAPI ... configured and ready to roll!
$this->get('instagram');
//..
?>
<?
//...
public function __construct($apiKey, $apiSecret, Request $request, Router $router) {
if(empty($apiKey)){
throw new \Exception("Error: __construct() - Configuration data is missing.");
}
$this->setApiKey($apiKey);
$this->setApiSecret($apiSecret);
$this->setApiCallbackFromSymfonyData($request, $router);
}
//...
?>
parameters:
client_id: xxxxx
client_secret: yyyyy
clops.instagram.api.class: Clops\InstagramBundle\InstagramAPI
services:
instagram:
scope: request
class: "%clops.instagram.api.class%"
arguments: ["%client_id%", "%client_secret%", @request, @router]
@wodka
Copy link

wodka commented Sep 3, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment