【问题标题】:Dependency installation failed! error while deploying laravel app to heroku依赖安装失败!将 laravel 应用程序部署到 heroku 时出错
【发布时间】:2019-11-23 07:11:43
【问题描述】:

在尝试将 laravel 应用程序部署到 Heroku 时,我开始遇到此依赖项安装失败!将我的 DB_CONNECTION 更改为 Postgresql 后出错。当我这样做时

git push heroku 大师

..........
remote:          - Installing jakub-onderka/php-console-color (v0.2): Loading from cache
remote:          - Installing nikic/php-parser (v4.3.0): Downloading (100%)
remote:          - Installing jakub-onderka/php-console-highlighter (v0.4): Loading from cache
remote:          - Installing dnoegel/php-xdg-base-dir (0.1): Loading from cache
remote:          - Installing psy/psysh (v0.9.9): Loading from cache
remote:          - Installing laravel/tinker (v1.0.10): Loading from cache
remote:          - Installing guzzlehttp/guzzle (6.4.1): Downloading (100%)
remote:          - Installing unicodeveloper/laravel-paystack (1.0.2): Loading from cache
remote:        Package silex/silex is abandoned, you should avoid using it. Use symfony/flex instead.
remote:        Generating optimized autoload files
remote:        > Illuminate\Foundation\ComposerScripts::postAutoloadDump
remote:        > @php artisan package:discover --ansi
remote:
remote:        In ConfigurationUrlParser.php line 132:
remote:
remote:          parse_url() expects parameter 1 to be string, array given
remote:
remote:
remote:        Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
remote:  !     WARNING: A post-autoload-dump script terminated with an error
remote:
remote:  !     ERROR: Dependency installation failed!
remote:  !
remote:  !     The 'composer install' process failed with an error. The cause
remote:  !     may be the download or installation of packages, or a pre- or
remote:  !     post-install hook (e.g. a 'post-install-cmd' item in 'scripts')
remote:  !     in your 'composer.json'.
remote:  !
remote:  !     Typical error cases are out-of-date or missing parts of code,
remote:  !     timeouts when making external connections, or memory limits.
remote:  !
remote:  !     Check the above error output closely to determine the cause of
remote:  !     the problem, ensure the code you're pushing is functioning
remote:  !     properly, and that all local changes are committed correctly.
remote:  !
remote:  !     For more information on builds for PHP on Heroku, refer to
remote:  !     https://devcenter.heroku.com/articles/php-support
remote:  !
remote:  !     REMINDER: the following warnings were emitted during the build;
remote:  !     check the details above, as they may be related to this error:
remote:  !     - A post-autoload-dump script terminated with an error
remote:
remote:  !     Push rejected, failed to compile PHP app.
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !       Push rejected to blemademo.
remote:
To https://git.heroku.com/blemademo.git
 ! [remote rejected]   master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/blemademo.git'

我已在 heroku 仪表板上将 Heroku Postgres Hobby Dev 配置为数据库,并在我的配置中设置了 DATABASE_URL。 在我的 laravel database.php 文件中,我在文件顶部有这个;

$host = env('DB_HOST', '127.0.0.1');
$database = env('DB_DATABASE', '');
$username = env('DB_USERNAME', 'forge');
$password = env('DB_PASSWORD', 'forge');

if($databaseUrl = getenv('DATABASE_URL')) {
    $url = parse_url($databaseUrl);

    $host = $url['host'];
    $username = $url['user'];
    $password = $url['pass'];
    $database = ltrim($url['path'], '/');
}

而我的默认数据库连接设置为:

'default' => 'pgsql'

然后在我的连接数组中:

'pgsql' => [
    'driver' => 'pgsql',
    'url' => $url,
    'host' => $host,
    'database' => $database,
    'username' => $username,
    'password' => $password,
    'charset' => 'utf8',
    'prefix' => '',
    'prefix_indexes' => true,
    'schema' => 'public',
    'sslmode' => 'prefer',
    'port' => env('DB_PORT', '5432'),
],

我已经尝试在谷歌上搜索解决方案,但没有运气。请问我该怎么做才能解决这个问题?

【问题讨论】:

    标签: postgresql laravel-5 heroku heroku-postgres


    【解决方案1】:

    $url 是一个数组; parse_url 正在返回一个关联数组。配置中的 url 键应该是表示 URL 的字符串,而不是数组。您正在分配'url' => $url,,所以'url' 是一个数组,而不是一个字符串。

    如果你想使用url,你只需要设置那个键,它就会从你提供给你的那个 URL 中解析其余的信息。

    “一些托管数据库提供商,例如 Heroku,提供了一个单一的数据库“URL”,它在一个字符串中包含了数据库的所有连接信息。”

    “为方便起见,Laravel 支持这些 URL 作为使用多个配置选项配置数据库的替代方案。如果存在 url(或相应的 DATABASE_URL 环境变量)配置选项,它将用于提取数据库连接和凭据信息。”

    Laravel 5.8 Docs - Database - Configuration - Configuration Using URLs

    【讨论】:

    • 感谢@lagbox。我该如何解决这个问题?
    • @banky 假设您使用的不是 Laravel 5 的过时版本,您只需设置 url,它就会将该 url 解析为您需要的部分......如果您打算自己设置主机和凭据,您不需要设置url ...url 键是可选的
    • 效果很好!感谢您的帮助@lagbox。我猜我的 laravel 版本(5.8)在解析 url 时的工作方式与我遵循的教程中的不同。
    猜你喜欢
    • 2019-06-07
    • 2014-10-16
    • 2015-05-09
    • 2014-01-08
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多