revolution/laravel-vercel-installer

1.2.2 2024-05-29 03:48 UTC

This package is auto-updated.

Last update: 2024-09-14 02:04:59 UTC


README

Maintainability Test Coverage

安装一些文件以在 Vercel 上运行 Laravel。

要求

  • PHP >= 8.2
  • Laravel >= 10.0

安装

composer require revolution/laravel-vercel-installer --dev

php artisan vercel:install

卸载

composer remove revolution/laravel-vercel-installer --dev

vercel.json

它可能无法与使用 vercel.json 中的 functionsrewrites 的新规范一起工作。

区域

https://vercel.com/docs/edge-network/regions

构建

vercel-php@0.7.1 是 PHP8.3

要使用其他版本,请检查此存储库。 https://github.com/vercel-community/php

路由

如果在 public 目录中还有其他文件,请将它们添加到 routes。

    {
        "src": "/images/(.*)",
        "dest": "/public/images/$1"
    },

环境变量

在 Vercel 设置页面设置秘密环境变量。

php artisan key:generate --show 命令生成一个新密钥,但不会更新 .env 文件。在设置页面设置此密钥 APP_KEY

数据库

您可以使用 Vercel Postgres 或 AWS RDS。

https://vercel.com/docs/storage/vercel-postgres

缓存和会话

您不能使用 file 驱动程序。

如果您使用数据库,则可以使用 database 驱动程序。

TrustProxies

Laravel 10

如果您遇到 TrustProxies 的问题,请更改 /app/Http/Middleware/TrustProxies.php

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array<int, string>|string|null
     */
    protected $proxies = '*';

Laravel 11

更改 /bootstrap/app.php

->withMiddleware(function (Middleware $middleware) {
        $middleware->trustProxies(at: '*');
    })

API 前缀

如果您使用 Laravel 的 API 路由,则需要更改 /api/ 路由,因为它与 Vercel 冲突。

Laravel 11

/bootstrap/app.php

use Illuminate\Foundation\Application;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        api: __DIR__.'/../routes/api.php',
        apiPrefix: 'prefix/api',
        health: '/up',
    )

许可证

MIT