Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use google-api-php-client on laravel 5.6 when there is no namespace

Sorry if this is a repeat but the few close questions I have found have not worked for me. I am a Laravel newbie. I am trying to use the github library https://github.com/google/google-api-php-client with laravel 5.6 framework.

I installed the api by running:

composer require google/apiclient:^2.0

My composer.json looks correct since it includes:

"require": {
    "php": "^7.1.3",
    "fideloper/proxy": "^4.0",
    "google/apiclient": "^2.2",
    "laravel/framework": "5.6.*",
    "laravel/tinker": "^1.0"
},

I have run composer update. I can see the Google folder in my Vendor's folder.

The github readme for this then says to add the autoloader include. However, this is not the way we do it for Laravel from my understanding. I have found that the Google_Client class has no namespace, so I don't know how to add it properly to the app.php file. Or to use it within the controller I want to. When I try to create a new Google_Client within the controller, it says it can't be found. If I try to 'use' it, I don't know what path to give it without the namespace present.

Editing to add more information for some of the comments below:

I run 'composer require google/apiclient:^2.0

The output:

vagrant@homestead:~/code/sageAnalytics$ composer require google/apiclient:^2.0 ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 10 installs, 0 updates, 0 removals
  - Installing psr/http-message (1.0.1): Loading from cache
  - Installing guzzlehttp/psr7 (1.4.2): Loading from cache
  - Installing guzzlehttp/promises (v1.3.1): Loading from cache
  - Installing guzzlehttp/guzzle (6.3.2): Loading from cache
  - Installing phpseclib/phpseclib (2.0.10): Loading from cache
  - Installing firebase/php-jwt (v5.0.0): Loading from cache
  - Installing google/apiclient-services (v0.53): Loading from cache
  - Installing psr/cache (1.0.1): Loading from cache
  - Installing google/auth (v1.2.1): Loading from cache
  - Installing google/apiclient (v2.2.1): Loading from cache phpseclib/phpseclib suggests installing ext-libsodium (SSH2/SFTP can make use of some algorithms provided by the libsodium-php extension.) phpseclib/phpseclib suggests installing ext-mcrypt (Install the Mcrypt extension in order to speed up a few other cryptographic operations.) phpseclib/phpseclib suggests installing ext-gmp (Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations.) google/apiclient suggests installing cache/filesystem-adapter (For caching certs and tokens (using Google_Client::setCache)) Writing lock file Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover Discovered Package: fideloper/proxy Discovered Package: laravel/scout Discovered Package: laravel/tinker Discovered Package: nunomaduro/collision Package manifest generated successfully.

Its not one of the packages it discovered - Im thinking it should be. My composer.json file looks like:

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
    "php": "^7.1.3",
    "fideloper/proxy": "^4.0",
    "google/apiclient": "^2.0",
    "laravel/framework": "5.6.*",
    "laravel/tinker": "^1.0"
},
"require-dev": {
    "filp/whoops": "^2.0",
    "fzaninotto/faker": "^1.4",
    "mockery/mockery": "^1.0",
    "nunomaduro/collision": "^2.0",
    "phpunit/phpunit": "^7.0"
},
"autoload": {
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},
"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    }
},
"extra": {
    "laravel": {
        "dont-discover": [
        ]
    }
},
"scripts": {
    "post-root-package-install": [
        "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "@php artisan key:generate"
    ],
    "post-autoload-dump": [
        "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
        "@php artisan package:discover"
    ]
},
"config": {
    "preferred-install": "dist",
    "sort-packages": true,
    "optimize-autoloader": true
},
"minimum-stability": "dev",
"prefer-stable": true

}

When I run my program and call on new Google_Client I see

enter image description here

So Im missing something in getting it to be discovered by the autoloader.
Running the command 'composer dumpautoload' gives me:

vagrant@homestead:~/code/sageAnalytics$ composer dumpautoload

Generating optimized autoload files

Illuminate\Foundation\ComposerScripts::postAutoloadDump @php artisan package:discover Discovered Package: fideloper/proxy Discovered Package: laravel/scout Discovered Package: laravel/tinker Discovered Package: nunomaduro/collision Package manifest generated successfully.

So again there is no mention of package google/apiclient. How do I get it to be found?

like image 295
Janelle Thorn Contreras Avatar asked Dec 03 '22 11:12

Janelle Thorn Contreras


1 Answers

If a class is not in any namespace, you can reference from inside a namespace by prefixing it with a leading \, as in $foo = new \Google_Client;

Meanwhile, the string you specify in a use statement isn't a "path" as such, it's just the "fully-qualified class name", that is, the class name with its namespace included on the front. So for a class in no namespace, it's just the name, use Google_Client;

like image 143
IMSoP Avatar answered Dec 23 '22 13:12

IMSoP