2016-07-07 6 views
0

을 사용하여 컨트롤러 외부에있는 PHP 함수를 호출하므로 Laravel 프로젝트가있는 나머지 메모리를 가져 오는이 함수가 있습니다. 나머지 메모리를 확인해야하는 컨트롤러가 두 개 있습니다. 여기laravel 4.2

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); 
} 

그래서 내가 그것을 필요로 할 때 난 그냥 전화를 할 수 있도록 외부 PHP에서 함수를 넣고 싶어 그냥 내 컨트롤러의 내부처럼 보이는 것입니다. 이 작업을 수행하는 방법에 대한 아이디어가 있습니까? 정말 고마워!

+0

그래서 기능을 사용하면 이동 하시겠습니까? 귀하의 코드 예제에서 두 가지를 볼 수 있습니다. – 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'; 
여기에서 찍은 당신의 설정에서 현재210

는 /이 서비스 프로 바이더를로드 app.php 당신은

'App\Providers\HelperServiceProvider', 

코드

를 완료 : How do I make global helper functions in laravel 5?