Laravel 在 aws lambda 上部署时出现外观问题

Laravel facade problem when deployed on aws lambda

我正在使用 laravel 8 和 bref 在 lambda 上部署它。在制作 cron 作业功能后发送电子邮件。我部署的时候,门面有问题

{
  "errorType": "RuntimeException",
  "errorMessage": "A facade root has not been set.",
  "stackTrace": [
    "#0 /var/task/app/functions/sendTestMail.php(11): Illuminate\Support\Facades\Facade::__callStatic()",
    "#1 /var/task/vendor/bref/bref/src/Runtime/Invoker.php(34): Bref\Runtime\FileHandlerLocator->App\Functions\{closure}()",
    "#2 /var/task/vendor/bref/bref/src/Runtime/LambdaRuntime.php(102): Bref\Runtime\Invoker->invoke()",
    "#3 /opt/bref/bootstrap.php(43): Bref\Runtime\LambdaRuntime->processNextEvent()",
    "#4 {main}"
  ]
}

这是我的目录结构和功能: sendTestMail.php

serverless.yml:

service: test
provider:
  name: aws
  # The AWS region in which to deploy (us-east-1 is the default)
  region: ap-southeast-1
  # The stage of the application, e.g. dev, production, staging… ('dev' is the default)
  stage: dev
  runtime: provided.al2

package:
  # Directories to exclude from deployment
  exclude:
    - node_modules/**
    - public/storage
    - resources/assets/**
    - storage/**
    - tests/**

functions:
  # This function runs the Laravel website/API
  web:
    handler: public/index.php
    timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
    layers:
      - ${bref:layer.php-80-fpm}
    events:
      - httpApi: "*"
  # This function lets us run artisan commands in Lambda
  artisan:
    handler: artisan
    timeout: 120 # in seconds
    layers:
      - ${bref:layer.php-80} # PHP
      - ${bref:layer.console} # The "console" layer
  cron:
    handler: app/functions/sendTestMail.php
    layers:
      - ${bref:layer.php-80}
    events:
      - schedule: rate(5 minutes)

plugins:
  # We need to include the Bref plugin
  - ./vendor/bref/bref

有人知道如何解决这个问题吗?顺便说一句,我如何在部署之前在我的本地机器上测试处理函数?谢谢

错误可以试试这个

  • php artisan config:cache
  • php artisan config:clear
  • php artisan cache:clear

我认为这是因为在您的网络函数中处理程序是 public/index.php。这会正确初始化 Laravel 应用程序。你的 cron 函数处理程序是 app/functions/sendTestMail.php 所以 index.php 永远不会被调用并且 Laravel 内核永远不会处理请求。

我目前没有很好的解决方案,因为我觉得它打破了 Laravel 中的许多最佳实践和规则,并且想用它进行更多试验。但我能够获取整个 index.php 内容并将其粘贴到 Lambda 调用的文件中我的 return 函数上方。

所以换句话说,我认为如果你粘贴这个

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;

use App\Models\Task;
define('LARAVEL_START', microtime(true));
if (file_exists(__DIR__.'/storage/framework/maintenance.php')) {
    require __DIR__.'/storage/framework/maintenance.php';
}
require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';
$kernel = $app->make(Kernel::class);

$response = tap($kernel->handle(
    $request = Request::capture()
))->send();

$kernel->terminate($request, $response);

作为您 app/functions/sendTestMail.php 文件中的第一件事,它可能会起作用。取决于您在中间件中编码的内容,因为它将首先 运行。

这在我的申请中对我有用。