- vừa được xem lúc

Apply enum in php 8 to laravel project with multi language

0 0 15

Người đăng: Trường Trần Văn

Theo Viblo Asia

Hôm nay mình sẽ hướng dẫn các bạn áp dụng enum trong php8 vào dự án với FW Laravel cho đa ngôn ngữ.

Tạo các phương thức

Trong enum chúng ta có thể tạo các phương thức

<?php enum Suit implements Colorful
{ case Hearts; case Diamonds; case Clubs; case Spades; // Fulfills the interface contract. public function color(): string { return match($this) { Suit::Hearts, Suit::Diamonds => 'Red', Suit::Clubs, Suit::Spades => 'Black', }; } // Not part of an interface; that's fine. public function shape(): string { return "Rectangle"; }
} function paint(Colorful $c) { ... } paint(Suit::Clubs); // Works print Suit::Diamonds->shape(); // prints "Rectangle"
?>

Đa ngôn ngữ

  1. Tạo các method
public static function asSelectArray() { $array = []; foreach(self::cases() as $item){ $array[$item->value] = $item->getDescription($item->value); } return $array; } public static function getLocalizationKey(): string { return 'enums.' . static::class; } protected static function getLocalizedDescription(mixed $value): ?string { $localizedStringKey = static::getLocalizationKey() . '.' . $value; if (Lang::has($localizedStringKey)) { return __($localizedStringKey); } return null; } protected static function getAttributeDescription($value){ return self::from($value)->name; } public function description(){ return self::getDescription($this->value); } public static function getDescription(mixed $value): string { return static::getLocalizedDescription($value) ?? static::getAttributeDescription($value); }
  1. Tạo file enums.php trong folder lang/local
return [ Status::class => [ Status::Active->value => 'Hoạt động', Status::Deactive->value => 'Tạm ngưng' ],
];

Validate với enum

use App\Enums\Status;
use Illuminate\Validation\Rules\Enum; $request->validate([ 'status' => [new Enum(Status::class)],
]);

Model với enum

<?php namespace App\Models; use App\Enums\Status;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; class Attribute extends Model
{ use HasFactory; protected $guarded = []; protected $casts = [ 'status' => Status::class ];
} 

Tối ưu

Trong enum hỗ trợ trait giúp chúng ta tái sử dụng các method hỗ trợ. Bây giờ chúng ta tạo file App\Support\Enum.php

<?php namespace App\Support;
use Illuminate\Support\Facades\Lang; trait Enum
{ public static function getCases(): array { return array_column(self::cases(), 'name'); } public static function getValues(): array { return array_column(self::cases(), 'value'); } public static function asSelectArray() { $array = []; foreach(self::cases() as $item){ $array[$item->value] = $item->getDescription($item->value); } return $array; } public static function getLocalizationKey(): string { return 'enums.' . static::class; } protected static function getLocalizedDescription(mixed $value): ?string { $localizedStringKey = static::getLocalizationKey() . '.' . $value; if (Lang::has($localizedStringKey)) { return __($localizedStringKey); } return null; } protected static function getAttributeDescription($value){ return self::from($value)->name; } public function description(){ return self::getDescription($this->value); } public static function getDescription(mixed $value): string { return static::getLocalizedDescription($value) ?? static::getAttributeDescription($value); }
}

Với cách này chúng ta có thể sử dụng bất cứ trong enum nào mà bạn cần mà không cần copy/paste. Và không bị lặp lại code, dễ dàng thay đổi đối với các method support cho enum Ở trên là ý tưởng của mình trong quá trình làm việc. Bạn nào có ý hay hơn thì cmt phía dưới nhé, đừng nén gạch 😃))

Bình luận

Bài viết tương tự

- vừa được xem lúc

Deploy project Laravel trên Railway

Xin chào mọi người, như mọi người có thể đã biết thì kể từ 28/11/2022 Heroku đã chính thức ngừng cung cấp các plan miễn phí (Free Heroku Postgres, free Heroku Data for Redis®, and free Heroku Dynos).

0 0 30

- vừa được xem lúc

Dockerize ứng dụng Laravel ─ Biện pháp triển khai, quản lý version Laravel đơn giản

Đặt vấn đề:. .

0 0 18

- vừa được xem lúc

Các bước deploy project lên AWS EC2 một cách đơn giản từ a đến á

I. Lời mở đầu.

0 0 14

- vừa được xem lúc

FilamentPhp: Lối đi mới cho lập trình viên PHP

PHP là một trong những ngôn ngữ lập trình web phổ biến nhất hiện nay. Trong những năm gần đây, PHP đã có sự phát triển vượt bậc với sự xuất hiện của nhiều framework mới, trong đó có FilamentPhp.

0 0 12

- vừa được xem lúc

Ứng dụng thời gian thực với Laravel Reverb

Giới thiệu. Ở bản cập nhật Laravel 11, Taylor Otwell đã giới thiệu một vài sự cập nhật mới mẻ cho Laravel.

0 0 9

- vừa được xem lúc

Hướng dẫn nhanh Laravel 11 với Servbay

Giới thiệu. Laravel 11 đã được phát hành, mang lại nhiều cải tiến.

0 0 11