【问题标题】:Useful helper functions in Laravel 4?Laravel 4 中有用的辅助函数?
【发布时间】:2015-12-07 12:23:55
【问题描述】:

你在 Laravel 中都使用了哪些有用的辅助函数?我目前只有两个,但我有兴趣了解人们过去创建了哪些类型的函数来激发未来的灵感。

我的应用中有这两个全局变量:

Blade::extend(function($value) {
   return preg_replace('/\@define(.+)/', '<?php ${1}; ?>', $value);
});

HTML::macro('raw', function($htmlbuilder) {
   return htmlspecialchars_decode($htmlbuilder);
});

【问题讨论】:

    标签: php function laravel laravel-4 laravel-5


    【解决方案1】:

    View 中多了一个 Helper 函数

     View::share('human_size', function($bytes) {
        $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
        for ($i = 0; $bytes > 1024; $i++) {
            $bytes /= 1024;
        }
        return round($bytes, 2).' '.$units[$i];
    });
    

    并像这样在刀片中调用:{{ $human_size(123456789) }}

    Some other function that created in the past to spark some inspiration into the future

    【讨论】:

      猜你喜欢
      • 2013-08-08
      • 2022-08-04
      • 1970-01-01
      • 2016-01-06
      • 2018-05-07
      • 2020-08-14
      • 2015-04-13
      • 2014-04-19
      • 1970-01-01
      相关资源
      最近更新 更多