【问题标题】:Laravel facade problem when deployed on aws lambda部署在 aws lambda 上时的 Laravel 外观问题
【发布时间】:2021-06-14 12:45:32
【问题描述】:

我正在使用 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 laravel lambda serverless laravel-facade


    【解决方案1】:

    你可以试试这个错误

    • php 工匠配置:缓存
    • php 工匠配置:清除
    • php artisan 缓存:清除

    【讨论】:

    • 我已经试过了,但是没用
    【解决方案2】:

    我认为这是由于在您的 Web 函数中处理程序是 public/index.php 的问题。这会正确初始化 Laravel 应用程序。您的 cron 函数处理程序是 app/functions/sendTestMail.php,因此 index.php 永远不会被调用,并且 Laravel 内核永远不会处理请求。

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

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

    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 文件中的第一件事,它可能会起作用。取决于您在中间件中编码的内容,因为它将首先运行。

    这在我的应用程序中对我有用。

    【讨论】:

      猜你喜欢
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多