Google Api does not load into my controller

Dear,

I’m trying to import data from a Google Sheet but when I try to create my google client I got this error message :

Class “App\Controller\Google\Client” not found📋

I well installed the plugin. It works when I use it on my view (and not from my controller)

I try to add require_once(vendor/autoload.php) but it still not working.

Any ideas ?

  1. you don’t need to manually add the vendor/autoload.php. This already happens in your webroot/index.php .
  2. Make sure to either use a FQCN like $client = new \Google\Client(); or add a use statement at the top of your class like
<?php
declare(strict_types=1);

namespace App\Controller;

use Cake\Controller\Controller;
use Google\Client;

class MyController extends Controller
{
    public function myGoogleAction() 
    {
        $client = new Client();
    }
}

Thanks it works for the client.

But I still have an issue for creating a google sheet with :

$service = new Google_Service_Sheets($client);

Same error :

Class “App\Controller\Google_Service_Sheets” not found

Oh I found it.

I just added

use Google\Service\Sheets;

Same problem. If you don’t start a class name with \ then its relative to your current namespace - therefore it tries to load the class App\Controller\Google_Service_Sheets.

But you need

$service = new \Google_Service_Sheets($client);

This is all very standard PHP namespace stuff, nothing whatsoever to do with CakePHP, in case you’re wondering why the Cake documentation doesn’t discuss it, or want to know how to Google up additional reading materials.