2016-07-07 19 views
0

を使用してコントローラの外にあるphp関数を呼び出すので、私のLaravelプロジェクトがある残りのメモリを取得するこの関数があります。残っているメモリをチェックする必要があるコントローラが2つあります。ここでlaravel 4.2

は、だから私は、私はそれを必要なときに、私はちょうどそれを呼び出すなるように、外部のPHPの関数を入れたかっただけで、私のコントローラ

private function convGB($bytes, $unit = "", $decimals = 2) 
{ 
    $units = array('B' => 0, 'KB' => 1, 'MB' => 2, 'GB' => 3, 'TB' => 4, 
    'PB' => 5, 'EB' => 6, 'ZB' => 7, 'YB' => 8); 

    $value = 0; 
    if ($bytes > 0) 
    { 
     if (!array_key_exists($unit, $units)) 
     { 
      $pow = floor(log($bytes)/log(1024)); 
      $unit = array_search($pow, $units); 
     } 

     $value = ($bytes/pow(1024,floor($units[$unit]))); 
    } 

    if (!is_numeric($decimals) || $decimals < 0) { 
    $decimals = 2; 
    } 

    return sprintf('%.' . $decimals . 'f '.$unit, $value); 
} 

private function getMem() 
{ 
    $ds = disk_total_space(substr(base_path(), 0, 2)); 
    $fs = disk_free_space(substr(base_path(), 0, 2)); 
    $ffs = disk_free_space(substr(base_path(), 0, 2)); 

    if ($ds >= 1073741824) 
    { 
     $ds = number_format($ds/1073741824, 2) . ' GB'; 
    } 
    elseif ($ds >= 1048576) 
    { 
     $ds = number_format($ds/1048576, 2) . ' MB'; 
    } 
    elseif ($ds >= 1024) 
    { 
     $ds = number_format($ds/1024, 2) . ' KB'; 
    } 
    elseif ($ds > 1) 
    { 
     $ds = $ds . ' B'; 
    } 
    elseif ($ds == 1) 
    { 
     $ds = $ds . ' B'; 
    } 
    else 
    { 
     $ds = '0 size'; 
    } 

    if ($fs >= 1073741824) 
    { 
     $fs = number_format($fs/1073741824, 2) . ' GB'; 
    } 
    elseif ($fs >= 1048576) 
    { 
     $fs = number_format($fs/1048576, 2) . ' MB'; 
    } 
    elseif ($fs >= 1024) 
    { 
     $fs = number_format($fs/1024, 2) . ' KB'; 
    } 
    elseif ($fs > 1) 
    { 
     $fs = $fs . ' B'; 
    } 
    elseif ($fs == 1) 
    { 
     $fs = $fs . ' B'; 
    } 
    else 
    { 
     $fs = '0 size'; 
    } 

    $converted = $this->convGB($ffs); 

    return array($ds , $fs , $converted); 
} 

の内側にあるように見えるものです。これを行う方法に関するアイデア?本当にありがとう!

+0

だから、どの機能を移動したいのですか?私はあなたのコードの例で2つを参照してください。 – JRSofty

+0

こんにちは私はちょうど 'getMem'関数を使用します – BourneShady

答えて

1

私のヘルパーのAnythingHelper.php例があるあなたのアプリ/ヘルパーのディレクトリ名でそれを新しいファイルを作成します。

<?php 
function getDomesticCities() 
{ 
$result = \App\Package::where('type', '=', 'domestic') 
    ->groupBy('from_city') 
    ->get(['from_city']); 

return $result; 
} 

は、コマンド

php artisan make:provider HelperServiceProvider 

に従うことによって、あなたのヘルパーのためのサービス・プロバイダーを生成新たに生成されたHelperServiceProvider.phpの登録機能で次のコードを追加します

require base_path().'/app/Helpers/AnythingHelper.php'; 

今、あなたのconfig/app.php負荷でこのサービスプロバイダとあなたが

'App\Providers\HelperServiceProvider', 

を行っているコードは、ここから取られた:How do I make global helper functions in laravel 5?