【Laravel】在 Facades 建立自定義的 Aliases

筆記

 

config/app.php

'aliases' => [
    ...
    'NumberCreater' => App\Support\Facades\NumberCreater::class,
]

 

App\Support\Facades\NumberCreater.php

namespace App\Support\Facades;

use Illuminate\Support\Facades\Facade;

class NumberCreater extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'number.creater';
    }
}

 

App\Support\NumberCreater\NumberCreater.php

namespace App\Support\NumberCreater;

use Illuminate\Support\Str;

class NumberCreater
{
    /**
     * @param String $orderId
     * @return string
     */
    public function getOrderNo(String $orderId): string
    {
        return str_pad($orderId, 8, "1", STR_PAD_LEFT);
    }
}

 

使用方式

NumberCreater::getOrderNo($orderId);