laravel-custom-helpers

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Custom Helpers

自定义辅助函数

Create a helper file

创建辅助函数文件

php
// app/Support/helpers.php
function money(int $cents): string { return number_format($cents / 100, 2); }
php
// app/Support/helpers.php
function money(int $cents): string { return number_format($cents / 100, 2); }

Autoload

自动加载

Add to
composer.json
:
json
{
  "autoload": { "files": ["app/Support/helpers.php"] }
}
Run
composer dump-autoload
.
composer.json
中添加:
json
{
  "autoload": { "files": ["app/Support/helpers.php"] }
}
运行
composer dump-autoload

Guidelines

指导原则

  • Keep helpers small and pure; avoid hidden IO/state
  • Prefer static methods on value objects when domain-specific
  • 保持辅助函数小巧且纯函数;避免隐藏的IO操作/状态
  • 针对特定领域场景,优先使用值对象的静态方法