mach3builders/laravel-privatelabel

为 mach3laravel 应用添加私有标签功能

v2.2 2024-04-02 11:55 UTC

This package is auto-updated.

Last update: 2024-09-02 12:48:37 UTC


README

此软件包是为 laravel-starter 设计的。它假定您的项目是使用 laravel-starter 模板启动的。

安装

通过 composer 安装软件包

composer require mach3builders/laravel-privatelabel

发布和执行迁移

php artisan vendor:publish --provider="Mach3builders\PrivateLabel\PrivateLabelServiceProvider" --tag="privatelabel-migrations"

发布配置文件

php artisan vendor:publish --provider="Mach3builders\PrivateLabel\PrivateLabelServiceProvider" --tag="privatelabel-config"

这是发布的配置文件

return [
    /**
     * The owner model of the private label
     */
    'owner_model' => App\Models\Account::class,

    /**
     * The layout the extend the views off
     */
    'extend_layout' => 'privatelabel::layout',

    /**
     * Middleware the private label route should live under
     */
    'middleware' => ['web', 'auth', 'locale'],

    /**
     * The prefix used inside the route group
     */
    'route_prefix' => 'app',

   /**
     * The domain that runs the main app this is used for the nginx template
     */
    'main_domain' => env('PRIVATE_LABEL_MAIN_DOMAIN'),
    
    /**
     * The domain every label needs to be cnamed to
     */
    'domain' => env('PRIVATE_LABEL_DOMAIN'),

    /**
     * Forge information
     */
    'forge' => [
        'api_token' => env('FORGE_API_TOKEN'),
        'server_id' => env('FORGE_SERVER_ID'),
        'server_ip' => env('FORGE_SERVER_IP'),
    ],

    /**
     * Forge information
     */
    'mailgun' => [
        'api_token' => env('MAILGUN_API_TOKEN', ''),
    ],
];

将以下变量添加到您的 .env 文件中

PRIVATE_LABEL_DOMAIN=
FORGE_SERVER_ID=
FORGE_API_TOKEN=
FORGE_SERVER_IP=
MAILGUN_API_TOKEN=

迁移数据库

php artisan migrate

用法

导航

将索引路由包含在您的菜单中

    <li class="nav-item">
        <a href="{{ route('private-label.index', {REPLACE_WITH_OWNER_MODEL}) }}" class="nav-link{{ Route::is('private-label.*') ? ' active' : '' }}">
            <span class="ui-icon-text">
                <i class="far fa-tag"></i>
                <span>{{ __('Private label') }}</span>
            </span>
        </a>
    </li>

PHP

将此特性集成到您的所有者模型中,如配置中指定

use Mach3builders\PrivateLabel\Traits\HasPrivateLabel;

use HasPrivateLabel;

JavaScript

将此片段插入到您的 app.js

import '@mach3builders/ui/dist/js/plugins/poller'

$('.private-label-poller').poller({
    running: function(target, data) {
        switch(data.current_status) {
            case 'dns_validating':
                $('#dns_validating').removeClass('d-none')
                $('#dns_validated').addClass('d-none')
            break;

            case 'dns_validated':
                $('#dns_validating').addClass('d-none')
                $('#dns_validated').removeClass('d-none')
            break;

            case 'site_installing':
                $('#site_installing').removeClass('d-none')
                $('#site_installed').addClass('d-none')
            break;
        }
    },
    done: function (target, data) {
        $('#site_installing').addClass('d-none')
        $('#site_installed').removeClass('d-none')
    }
})

品牌定制

要轻松管理品牌特定元素,如徽标和favicons,请使用此 Brand.php 模板

<?php

namespace App;

class Brand
{
    public static function name()
    {
        return label()->name
            ?? config('app.name', 'Mach3Builders');
    }

    public static function logoLight()
    {
        return optional(label())->hasMedia('logo_light')
            ? label()->getFirstMediaUrl('logo_light')
            : config('brand.logo_light');
    }

    public static function logoDark()
    {
        return optional(label())->hasMedia('logo_dark')
            ? label()->getFirstMediaUrl('logo_dark')
            : config('brand.logo_dark');
    }

    public static function logoLoginHeight()
    {
        return optional(label())->hasMedia('logo_dark')
            ? label()->logo_login_height
            : config('brand.logo_login_height');
    }

    public static function logoAppHeight()
    {
        return optional(label())->hasMedia('logo_light')
            ? label()->logo_app_height
            : config('brand.logo_app_height');
    }

    public static function favicon()
    {
        return optional(label())->hasMedia('favicon')
            ? label()->getFirstMediaUrl('favicon')
            : config('brand.favicon');
    }

    public static function registration()
    {
        return config('brand.registration');
    }
}

标签API

此软件包提供了 label() 辅助函数。以下是您可以访问的方法和属性

属性

public string $domain;
public string $name;
public string $email;
public string $logo_login_height; // should be used together with the logo_login
public string $logo_app_height; // should be used together with the logo_app
public string $status; // has one of the following statusses

protected $statusses = [
    'dns_validating',
    'dns_validated',
    'site_installing',
    'site_installed',
];

事件

电子邮件页面允许用户将其电子邮件添加到他们的私有标签。然后,该电子邮件的域将被添加到m3b的mailgun帐户中。用户随后可以验证该域已被添加且已验证。

在域验证后,将触发 EmailDomainVerified 事件。请参阅以下示例,了解如何监听此事件。

protected $listen = [
    // ...
    \Mach3builders\PrivateLabel\Events\EmailDomainVerified::class => [
        \App\Listeners\Listener::class,
    ],
];

授权

使用 viewPrivateLabel 门来安全访问。将其添加到您的 AuthServiceProvider.php

function boot() 
{
    // ...
    return Gate::define('viewPrivateLabel', function ($user, $owner_id) {
        return app()->environment(['local', 'testing']);
    });
    // ...
}

方法

以下方法返回私有标签的所有者。这对应于配置中设置的所有者模型

public function owner()

由于laravel-privatelabel使用spatie/medialibary,因此所有方法都可用。以下集合在 PrivateLabel 模型内部定义

public function registerMediaCollections(): void
{
    $this->addMediaCollection('logo_light')->singleFile();
    $this->addMediaCollection('logo_dark')->singleFile();
    $this->addMediaCollection('favicon')->singleFile();
}

命令

该软件包包含2个命令,一个用于更新所有私有标签的php版本,另一个用于在forge服务器上重新安装所有私有标签。

重新安装标签

以下命令将在forge服务器上重新安装所有私有标签。私有标签将被更新为状态 dns_validating 并在服务器上安装。

要重新安装所有标签,请使用

php artisan label:reinstall

要重新安装特定标签,请使用

php artisan label:reinstall --label=LABEL_ID

更新标签php版本

以下命令将更新所有私有标签的php版本。php版本将被更新为forge服务器上可用的最新版本。或者,当运行命令时,用户会被提示指定版本

php artisan label:update-php

测试

composer test

代码风格

所有代码都应使用以下命令进行格式化

composer pint