Question

How do I connect to Cloudant using Sag in my Symfony 2 project?

Was it helpful?

Solution

  1. Configure Cloudant (create DB, API keys, etc.).
  2. Add the following configuration to app/config/parameters.yml.dist:

    cloudant_hostname:    ~
    cloudant_port:        443
    cloudant_use_ssl:     true
    
    cloudant_database:    ~
    cloudant_username:    ~
    cloudant_password:    ~
    
    • If preferred, you can specify default values instead of "~".
  3. Install sag:

    > composer.phar require "sag/sag":"dev-master"
    
    • At the end of this step, Composer will prompt you to enter values for the parameters listed above.
  4. Define the service in your bundle's services.yml file:

    services:
      cloudant:
        class:  Sag
        arguments:
            - "%cloudant_hostname%"
            - "%cloudant_port%"
        calls:
            - [useSSL, ["%cloudant_use_ssl%"]]
            - [setDatabase, ["%cloudant_database%"]]
            - [login, ["%cloudant_username%", "%cloudant_password%"]]
    
  5. Access the service in your controller:

    /** @var \Sag $sag */
    $sag = $this->get('cloudant');
    
    /* You're now good to go! */
    $post = $sag->get('postID')->body;
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top