没有命名空间时如何在 Laravel 5.6 上使用 google-api-php-client
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49639801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use google-api-php-client on laravel 5.6 when there is no namespace
提问by Janelle Thorn Contreras
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-clientwith laravel 5.6 framework.
对不起,如果这是重复,但我发现的几个接近的问题对我没有用。我是 Laravel 新手。我正在尝试将 github 库https://github.com/google/google-api-php-client与 laravel 5.6 框架一起使用。
I installed the api by running:
我通过运行安装了 api:
composer require google/apiclient:^2.0
作曲家需要 google/apiclient:^2.0
My composer.json looks correct since it includes:
我的 composer.json 看起来是正确的,因为它包括:
"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.
我已经运行作曲家更新。我可以在我的供应商文件夹中看到 Google 文件夹。
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.
github自述文件然后说要添加自动加载器包含。然而,根据我的理解,这不是我们为 Laravel 做的方式。我发现 Google_Client 类没有命名空间,所以我不知道如何将它正确添加到 app.php 文件中。或者在我想要的控制器中使用它。当我尝试在控制器中创建一个新的 Google_Client 时,它说找不到它。如果我尝试“使用”它,我不知道在没有命名空间的情况下给它什么路径。
Editing to add more information for some of the comments below:
编辑为下面的一些评论添加更多信息:
I run 'composer require google/apiclient:^2.0
我运行'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:
它不是它发现的软件包之一 - 我认为它应该是。我的 composer.json 文件看起来像:
{
"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
当我运行我的程序并调用新的 Google_Client 时,我看到
So Im missing something in getting it to be discovered by the autoloader.
Running the command 'composer dumpautoload' gives me:
所以我在让它被自动加载器发现时遗漏了一些东西。
运行命令“composer dumpautoload”给了我:
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.
Illuminate\Foundation\ComposerScripts::postAutoloadDump @php artisan package:discover 发现包:fideloper/proxy 发现包:laravel/scout 发现包:laravel/tinker 发现包:nunomaduro/collision 包清单生成成功。
So again there is no mention of package google/apiclient. How do I get it to be found?
所以再次没有提到包 google/apiclient。我如何才能找到它?
采纳答案by Janelle Thorn Contreras
The answer was posted in the comments at the top for the first possible answer by @Razor. I had messed up the project in some way most likely by messing with the autoload in composer.json. I deleted the Vendor folder completely and re-ran composer install. All works now.
答案已发布在@Razor 第一个可能答案的顶部评论中。我以某种方式搞砸了项目,很可能是通过搞乱 composer.json 中的自动加载。我完全删除了 Vendor 文件夹并重新运行 composer install。现在一切正常。
回答by IMSoP
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;
如果一个类不在任何命名空间中,您可以通过在命名空间内部添加前导前缀来引用它\
,如$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;
同时,您在use
语句中指定的字符串不是“路径”本身,它只是“完全限定的类名”,即前面包含其命名空间的类名。所以对于没有命名空间的类,它只是名称,use Google_Client;
回答by tushar zore
check the file composer.json
检查文件 composer.json
and add "vendor/google/apiclient/src/Google" in classmap array if not exist.
如果不存在,则在 classmap 数组中添加“vendor/google/apiclient/src/Google”。
"autoload": {
"classmap": [
"vendor/google/apiclient/src/Google"
]
}
and run composer dump-autoload
并运行 composer dump-autoload
No need of use
or require_once
statement.
不需要use
或require_once
声明。
the autoload array in composer.json makes ready all the files for you.
composer.json 中的自动加载数组为您准备好所有文件。
回答by Manuel Alejandro Caetano
Since you are working in Laravel, there is probably a bit of extra handiwork required to register the google api classes within your services. I found this https://github.com/pulkitjalan/google-apiclient, which might help you find a little bit more detail into installing a google api package that manages it specifically aimed at laravel.
由于您在 Laravel 中工作,因此在您的服务中注册 google api 类可能需要一些额外的工作。我找到了这个https://github.com/pulkitjalan/google-apiclient,它可能会帮助您找到更多详细信息来安装专门针对 Laravel 管理它的 google api 包。