Symfony6+PHP8集成kreait/firebase-bundle时凭证文件不存在错误求助
问题场景
使用Symfony 6 + PHP 8环境,通过kreait/firebase-bundle实现从Firebase获取用户列表并集成到API Platform中。调用API路径时触发错误:
Invalid service account: The file at 'firebase_credentials.json' does not exist
已通过firebase.yaml和.env.local配置路径,但问题仍未解决,相关配置及代码如下:
composer.json 依赖配置
// composer.json { "require": { "php": ">=8.1", "ext-ctype": "*", "ext-iconv": "*", "api-platform/core": "^2.6", "doctrine/annotations": "^1.0", "doctrine/doctrine-bundle": "^2.7", "doctrine/doctrine-migrations-bundle": "^3.2", "doctrine/orm": "^2.12", "kreait/firebase-bundle": "^4.1", "lexik/jwt-authentication-bundle": "^2.16", "nelmio/cors-bundle": "^2.2", "phpdocumentor/reflection-docblock": "^5.3", "phpstan/phpdoc-parser": "^1.6", "sensio/framework-extra-bundle": "^6.1", "symfony/asset": "6.1.*", "symfony/console": "6.1.*", "symfony/doctrine-messenger": "6.1.*", "symfony/dotenv": "6.1.*", "symfony/expression-language": "6.1.*", "symfony/flex": "^2", "symfony/form": "6.1.*", "symfony/framework-bundle": "6.1.*", "symfony/http-client": "6.1.*", "symfony/intl": "6.1.*", "symfony/mailer": "6.1.*", "symfony/mime": "6.1.*", "symfony/monolog-bundle": "^3.0", "symfony/notifier": "6.1.*", "symfony/process": "6.1.*", "symfony/property-access": "6.1.*", "symfony/property-info": "6.1.*", "symfony/proxy-manager-bridge": "6.1.*", "symfony/runtime": "6.1.*", "symfony/security-bundle": "6.1.*", "symfony/serializer": "6.1.*", "symfony/string": "6.1.*", "symfony/translation": "6.1.*", "symfony/twig-bundle": "6.1.*", "symfony/validator": "6.1.*", "symfony/web-link": "6.1.*", "symfony/yaml": "6.1.*", "twig/extra-bundle": "^2.12|^3.0", "twig/twig": "^2.12|^3.0" } }
Firebase 配置文件(firebase.yaml)
// my_project/config/packages/firebase.yaml kreait_firebase: projects: my_project: public: true default: true credentials: '%env(FIREBASE_CREDENTIALS)%' verifier_cache: cache.app auth_token_cache: cache.app
环境变量配置(.env.local)
// .env.local, json file is at root of project ###> kreait/firebase-bundle ### FIREBASE_CREDENTIALS="firebase_credentials.json" ###< kreait/firebase-bundle ###
API Platform 数据提供者代码
// FirebaseDataProvider for FirebaseUser entity endpoint use Kreait\Firebase\Contract\Auth; class FirebaseDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface { private Auth $auth; public function __construct(Auth $firebase) { $this->auth = $firebase; } public function getCollection(string $resourceClass, string $operationName = null, array $context = []) { dd($this->auth->listUsers()); } public function supports(string $resourceClass, string $operationName = null, array $context = []): bool { return $resourceClass === FirebaseUser::class; } }
排查与解决方法
1. 修正路径引用方式
Symfony环境变量中的相对路径默认指向项目public目录,而非根目录。如果凭证文件放在项目根目录,修改.env.local用绝对路径参数:
FIREBASE_CREDENTIALS="%kernel.project_dir%/firebase_credentials.json"
2. 检查文件权限
确保PHP进程能读取凭证文件:
chmod 644 firebase_credentials.json
3. 清除缓存并重启服务
Symfony会缓存环境变量,修改配置后执行缓存清理:
# 开发环境 php bin/console cache:clear # 生产环境 php bin/console cache:clear --env=prod
如果用内置服务器,重启服务确保新配置生效。
4. 验证文件实际路径
临时在FirebaseDataProvider中添加路径打印,确认文件位置:
public function __construct(Auth $firebase, string $projectDir) { $this->auth = $firebase; var_dump($projectDir . '/firebase_credentials.json'); exit; }
调用API查看输出路径,确认文件是否真实存在。
5. 检查Bundle配置加载情况
执行命令查看FireBundle的实际配置,确认凭证路径是否正确:
php bin/console config:dump kreait_firebase
内容的提问来源于stack exchange,提问作者Sheitak
相关产品推荐
相关产品推荐

