在Laravel Vapor上使用Laravel Echo(推子)

我很难确定为什么我的应用程序通过Laravel Vapor部署时,前端(Laravel Echo + Pusher)中的事件没有被“触发”,即使它在本地运行得很好。

Pushers debug console实际上表明,事件实际上是由Laravel应用程序分派给Pusher的。

由于我几乎浪费了半天的时间才发现问题所在(幸运的是,我看到了我的消息在本地环境中实时显示,而不是在登台之后才进行登台),所以我会再花10分钟在这里写一篇文章,这样一些人(希望)不需要浪费太多时间。

jxl8411185 回答:在Laravel Vapor上使用Laravel Echo(推子)

我已经花了几个小时试图弄清同一问题,并且正要失去它。

我刚刚发现这篇文章描述了一个有助于解决此问题的npm软件包。

来源 https://codinglabs.com.au/blog/2019/8/using-environment-specific-variables-in-laravel-vapor-with-mix

NPM软件包 https://laravel-mix.com/extensions/env-file

,

实际上是以下问题:

  • Vapor首先在.vapor目录中本地构建应用程序。
  • 在此构建过程中加载的.env中的PUSHER密钥是您本地.env(!)中的密钥,这意味着如果您设置MIX_PUSHER_APP_KEY不会改变任何内容和MIX_PUSHER_APP_CLUSTER中的.env.{environment}环境变量(通过运行vapor env:pull {environment}获得。

我以一种快速(又肮脏)的方式解决了这个问题:

  • 添加更多环境变量:
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=eu

# Necessary in `php artisan pusher:credentials`
PUSHER_LOCAL_APP_KEY=
PUSHER_LOCAL_APP_CLUSTER=eu

# Necessary in `php artisan pusher:credentials`
PUSHER_STAGING_APP_KEY=
PUSHER_STAGING_APP_CLUSTER=eu

# Necessary in `php artisan pusher:credentials`
PUSHER_PRODUCTION_APP_KEY=
PUSHER_PRODUCTION_APP_CLUSTER=eu

MIX_PUSHER_APP_KEY="${PUSHER_LOCAL_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_LOCAL_APP_CLUSTER}"
  • yarn run production之前的Vapor构建过程中添加一个新命令:php artisan pusher:credentials {environment}。因此,要进行登台,您将使用php artisan pusher:credentials staging

命令如下:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Support\Str;

final class SwapPusherCredentials extends Command
{
    use ConfirmableTrait;

    private array $keyPatterns = [
        'PUSHER_%sAPP_KEY','PUSHER_%sAPP_CLUSTER',];

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'pusher:credentials {environment=production}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sets the pusher credentials based on the current environment';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @throws \Exception
     */
    public function handle()
    {
        // Basically we're fixing a bug (?) in Vapor. Since we're building the front end locally,// we need to swap the pusher keys before building. If we don't do this,the front end
        // will be built with local pusher keys. When posting messages not local it's broken
        $environment = Str::upper($this->argument('environment'));

        $this->updatePusherEnvironmentVariables($environment);
    }

    /**
     * @param string $environment
     * @throws \Exception
     */
    private function updatePusherEnvironmentVariables(string $environment)
    {
        foreach ($this->keyPatterns as $pattern) {
            // 'PUSHER_LOCAL_APP_KEY' || 'PUSHER_STAGING_APP_KEY' etc.
            $variableToSet = sprintf($pattern,$environment . '_');

            // 'PUSHER_APP_KEY'
            $targetVariableName = sprintf($pattern,'');

            if (!env($targetVariableName,false)) {
                throw new \Exception('Missing environment value for ' . $targetVariableName);
            }

            $this->setVariable($targetVariableName,$variableToSet);
        }
    }

    private function setVariable(string $targetVariableName,string $variableToSet)
    {
        file_put_contents($this->laravel->environmentFilePath(),preg_replace(
            $this->replacementPattern($targetVariableName),$replacement = '${' . $variableToSet . '}',file_get_contents($this->laravel->environmentFilePath())
        ));

        $this->info("Successfully set MIX_{$targetVariableName} to {$replacement}!");
    }

    private function replacementPattern(string $targetVariableName)
    {
        // Don't remove notsurebutfixes,when removed it doesn't match the first entry
        // So it will match all keys except PUSHER_LOCAL_* for some reason.
        $escaped = '\=notsurebutfixes|\$\{' . $this->insertEnvironmentIntoKey('LOCAL',$targetVariableName) . '\}|\$\{' . $this->insertEnvironmentIntoKey('STAGING',$targetVariableName) . '\}|\$\{' . $this->insertEnvironmentIntoKey('PRODUCTION',$targetVariableName) . '\}';

        return "/^MIX_$targetVariableName{$escaped}/m";
    }

    // Insert the environment after PUSHER_ in any PUSHER_* like variable passed in.
    // So basically PUSHER_APP_KEY => PUSHER_LOCAL_APP_KEY
    private function insertEnvironmentIntoKey($environment,$key)
    {
        $parts = explode('_',$key,2);

        return $parts[0] . '_' . $environment . '_' . $parts[1];
    }
}

就是这样。现在,.env目录中的.vapor文件将在部署过程中更新为使​​用PUSHER_STAGING_APP_KEYPUSHER_STAGING_APP_CLUSTER

本文链接:https://www.f2er.com/2500664.html

大家都在问