很抱歉,如果这是重复的,但我发现的几个关闭问题对我没有用。我是 Laravel 新手。我正在尝试使用 github 库 https://github.com/google/google-api-php-client使用 laravel 5.6 框架。
我通过运行安装了 api:
Composer 需要 google/apiclient:^2.0
我的 composer.json 看起来是正确的,因为它包括:
"require": {
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"google/apiclient": "^2.2",
"laravel/framework": "5.6.*",
"laravel/tinker": "^1.0"
},
我已经运行了 composer update。我可以在供应商的文件夹中看到 Google 文件夹。
github 自述文件然后说要添加自动加载器包含。但是,根据我的理解,这不是我们为 Laravel 做的方式。我发现 Google_Client 类没有命名空间,所以我不知道如何将它正确添加到 app.php 文件中。或者在我想要的 Controller 中使用它。当我尝试在 Controller 中创建一个新的 Google_Client 时,它说找不到。如果我尝试“使用”它,我不知道在没有命名空间的情况下给它什么路径。
编辑以添加以下评论的更多信息:
我运行'composer require google/apiclient:^2.0
输出:
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.
它不是它发现的包之一 - 我认为它应该是。我的 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
当我运行程序并调用新的 Google_Client 时,我看到了
所以我错过了让自动加载器发现它的一些东西。
运行命令 'composer dumpautoload' 给我:
vagrant@homestead:~/code/sageAnalytics$ composer dumpautoload
生成优化的自动加载文件
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.
所以再次没有提到包 google/apiclient。我怎样才能找到它?
最佳答案
如果一个类不在任何命名空间中,您可以通过在命名空间内添加前缀 \
来引用它,如 $foo = new\Google_Client;
同时,您在 use
语句中指定的字符串本身并不是“路径”,它只是“完全限定类名”,即类名及其命名空间包括在前面。所以对于没有命名空间的类,它只是名称,use Google_Client;
关于php - 没有命名空间时如何在 laravel 5.6 上使用 google-api-php-client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49639801/