Install & Using Google API in Yii2 using Composer

Many of us are still going in the process of learning Yii2 and how to use composer with Yii2 to include packages. Today I am explaining about how to install and use Google API in Yii2 using Composer.

Using Composer may seems to be confusing in the beginning, but in reality its not. Composer really makes the library management and dependency checking not at all a headache for the developer.

Lets start with the Tutorial on installing Google API using Composer and using it in your Yii2 Application. First of all we have to install the Yii2 Application using composer. For those who are new you can see the tutorial on installing Yii2 using composer here.

Once your Yii2 Application is installed, you can install the Google API library for PHP using composer by editing the composer.json file in the root folder of your Yii2 application.

You have to modify the composer.json file and add “google/apiclient”: “1.0.*@beta” in the require block. Once you have saved the file you have to open your composer console and run the command composer update. This command will update all the exisiting libraries and also install the latest Google API library in your Yii2 project. You can verify by going into the vendor folder in your root folder and you will see a folder ‘google’ which consists all the library files for Google API.

Now you have the library files within your project and we can start using it in your Yii2 Project. All the libraries installed via Composer is automatically loaded in your Yii2 Application. So there is no need to include new library in your action.

You can simply create objects of your library classes as usual. Since Yii2 is using namespaces you have to added ‘’ before the class name. That is the only change in Yii2 while using a thrid party library.

On a special note I noticed most of the class names given in the Google API documentation is using old class names. Google havn’t updated the official documentation to use the new class name. So do not get panic if you see Class not found error while you use code from the Google API documentation. You can check the new class names in the source file inside vendorgoogleapiclientsrcGoogle folder. The new class names looks similar to the old name. but each words are now separated by underscore.

is now

Hope this article helps you in inegrating Google API in your Yii2 Application. If you are having any queries just let me know via comments.

8 thoughts on “Install & Using Google API in Yii2 using Composer”

  1. hello,
    I need your help .i have installed same extension but how can include and use this library.
    i am try my self many times but fails if you guide me please give me one hint use one function in controller or model

  2. Hi,
    thanks for your tutorial, but I got an error-message.
    composer add a subdirectory under vendor.

    When I tried to use it in my SiteController.php I got following message
    public function actionIndex()
    {

    $client = new Google_Client();
    }

    Error
    Class ‘app\controllers\Google_Client’ not found

    do you know, where is the error

    1. This is because of the namespace issue. Just replace with this line of code.

      $client = new \Google_Client();

      Let me know that helps you.

      Thanks

Leave a Reply