我正在按照Symfony入门文档构建一个非常简单的新项目,它由一个页面组成,其中显示了一个“幸运”随机数。但是,当我试图浏览到幸运号网页时,会发现一个关于缺少数据库驱动程序的错误。这似乎很奇怪,因为我在新项目中没有做任何与数据库相关的活动。数据库驱动程序错误来自何处,如何解决?
详细信息
我正在使用Windows 10工作站。我已经安装了PHP8,Composer,Symfony 5,我使用symfony new test --full和设置创建了一个新项目。我按照创建页面创建了一个路由和一个控制器
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
/**
* Description of LuckyController
*
* @author
*/
class LuckyController {
public function number(): Response
{
$number = random_int(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}#index:
# path: /
# controller: App\Controller\DefaultController::index
app_lucky_number:
path: /lucky/number
controller: App\Controller\LuckyController::number(上图) routes.yaml
当我在我的项目文件夹symfony server:start中启动symfony web服务器并打开我的浏览器到幸运号页http://localhost:8000/lucky/number时,我会得到一个关于缺少数据库驱动程序的错误:
D:\Web\test\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractPostgreSQLDriver.php第102行中的应用程序9月20日11:37:37:01非议PHP异常原则\DBAL\\DriverException:“在驱动程序中发生异常:无法找到驱动程序”
当我没有用这个网站建立任何数据库功能的时候,为什么我会得到一个数据库连接错误?我所建立的只是样本页,应该只是给我一个幸运的随机数。
我注意到,对于一个示例.env数据库连接,我的PostgreSQL文件有一个未注释的行。在Doctrine默认情况下需要数据库连接时,我更新了.env文件以指向mySQL上的空本地测试数据库:
###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7"
# DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=13&charset=utf8"
DATABASE_URL="mysql://test_webuser:#############@127.0.0.1:3306/test"
###< doctrine/doctrine-bundle ###(上图) .env文件(密码修改.)
然而,当我重新启动我的web服务器并浏览到幸运号页面时,我仍然会得到相同的驱动程序错误,并且它仍然在AbstractPostgreSQLDriver.php。我不知道PostgreSQL是从哪里来的。
我已经多次检查过Symfony入门文档,而且我似乎没有遗漏任何东西。我只是在创建一个“你好世界”类型页面的第一部分,所以我不确定有什么值得错过的……
如有任何建议,请告诉我如何克服这个错误。谢谢。
More Yaml
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '13'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App(上图)config/packages/配理。
# see https://symfony.com/doc/current/reference/configuration/framework.html
framework:
secret: '%env(APP_SECRET)%'
#csrf_protection: true
http_method_override: false
# Enables session support. Note that the session will ONLY be started if you read or write from it.
# Remove or comment this section to explicitly disable session support.
session:
handler_id: null
cookie_secure: auto
cookie_samesite: lax
storage_factory_id: session.storage.factory.native
#esi: true
#fragments: true
php_errors:
log: true
when@test:
framework:
test: true
session:
storage_factory_id: session.storage.factory.mock_file(上图) config/packages/framework.yaml work.yaml
security:
# https://symfony.com/doc/current/security/authenticator_manager.html
enable_authenticator_manager: true
# https://symfony.com/doc/current/security.html#c-hashing-passwords
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
users_in_memory: { memory: null }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: users_in_memory
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#firewalls-authentication
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }(上图) config/packages/security.yaml
发布于 2021-09-20 04:03:13
我的问题似乎是symfony服务器启动时的PostgreSQL错误的重复,我能够根据问题的答案创建一个有效的数据库连接来解决这个问题。(我第一次尝试在这个问题中创建数据库连接URL时,密码中的URL字符是无效的,但一旦我确定该站点正常工作,并且能够得到我的幸运号)。我没有足够的声誉点来标记这个问题为重复。
https://stackoverflow.com/questions/69248064
复制相似问题