1

basically I am trying to integrate the thrid party api in symfony2 application. I have the php class from api documention. I want integrate that in my custom bundle controller.

I also looking to add the option to configure the api key and secrets. I don't know how to start. Please look at below that i want to do

Example:-

namespace Acme\DemoBundle\Controller;

class DemoController extends Controller
{
    public function indexAction()
    {

    $myApi = new MyApi($key,$secret); // this is the stuff, I am trying to do!

        return array();
    }
}

First I try to create the library format symfony package. I don't know how to achive this this type. Can Some one tell me what are thinks I need to do.

1
  • Hey! may I know how you implemented this please! Commented Sep 10, 2015 at 12:14

2 Answers 2

6

TO do this in a proper way, you have to declare you api class as a service & use dependancy injection to inject your parameters :

parameters:
    your_api.class:      Acme\HelloBundle\Lib\YourApiClass
    your_api.key:        "key value for your api"
    your_api.token:      "token value for your api"

services:
   yourApiService:
       class:        "%your_api.class%"
       arguments:    ["%your_api.key%", "%your_api.token%"]

And in your controller & many other places you'll have access like that :

$api = $this->get('yourApiService');

Take a look for more informations : http://symfony.com/doc/current/book/service_container.html

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for reply. did you checked this repo. It's not a normal bundle. So I am not getting how to declare the services. how I need to alter the package, can you add some more notes
You can include this repo with your composer.json file by adding this line : rootaccez/odeskapi to requirements. After run a composer update & declare the api class as a service. Composer is the packager manager used by symfony ;) getcomposer.org
Fabien, I think your not getting. I created that package. I know how to install. Where can I add the service in that package. I think this service noramlly come in example:-AcmeHelloBundle/Resources/config/services.yml right. How can i add that in that package
If your package is not a bundle, you can't declare your lib a service. The application which include this package have to do that.
0

you can use it as a service: http://symfony.com/doc/current/book/service_container.html when i try to implement an extern api, i think about adapter pattern: http://en.wikipedia.org/wiki/Adapter_pattern

3 Comments

can you elaborate if you can. I have some understand with services. But did you checked the repo that i linked in question. Do you i need to create the bundle.
Whitout create the bundle you can use it. A service is a simple object who you can find any where in you applivatoom
By using dependency injection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.