2016-03-15 148 views
1

我有以下功能:Laravel未定义功能

# Get Directories of Path as Array 
function dirToArray($dir) { 

    $result = array(); 

    $cdir = scandir($dir); 
    foreach ($cdir as $key => $value) 
    { 
     if (!in_array($value,array(".",".."))) 
     { 
     if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) 
     { 
      $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value); 
     } 
     else 
     { 
      $result[] = $value; 
     } 
     } 
    } 

    return $result; 
} 

,并调用它像这样:

 $watchFolder = 'C:\\xampp\\htdocs\\project\\One\\'; 
     $this->dirToArray($watchFolder); 

,但我得到以下错误:

Call to undefined function App\Http\Controllers\dirToArray()

有谁知道如何解决这个?

编辑: 所以我充满控制器看起来是这样的:

<?php 

namespace App\Http\Controllers; 

use DB; 
use Mail; 
use File; 
use Input; 
use Request; 
use Illuminate\Filesystem\Filesystem; 
use App\Http\Controllers\Controller; 

class ProductRController extends Controller 
{ 

    public function createProduct() 
    { 
      ... 
      $watchFolder = 'C:\\xampp\\htdocs\\Product\\R\\'; 
      $this->dirToArray($watchFolder); 

      return redirect()->route('root')->with('message', 'Success')->withInput(); 
     } 
     else 
     { 
      return redirect()->route('newProduct')->with('message', 'Error')->withInput(); 
     } 

    } 

    # Get Directories of Path as Array 
    function dirToArray($dir) { 

     $result = array(); 

     $cdir = scandir($dir); 
     foreach ($cdir as $key => $value) 
     { 
      if (!in_array($value,array(".",".."))) 
      { 
      if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) 
      { 
       $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value); 
      } 
      else 
      { 
       $result[] = $value; 
      } 
      } 
     } 

     return $result; 
    } 

} 
+0

你在哪里把你的功能? –

+0

我把它放在一个控制器 –

+0

另外你在哪里把它的内部?它也不会在工作/不工作水平关系在这里但它是一个最佳实践(和PHP标准)显式声明的方法为'public' –

回答

0

你仍然需要你这样做是为了使用$this->一个函数内部的功能外,因为$this代表你调用哪个对象你的方法:

$result[$value] = $this->dirToArray($dir . DIRECTORY_SEPARATOR . $value); 
+1

谢谢,对不起,没看的时候也不 –

+0

谢谢你,约翰的名字。 –

+0

仅提供代码解答。如果这将被选为“最佳答案”,请更新它,并提供有关问题发生原因的信息以及解决问题的方法。如果此答案更新,我可以删除我的答案。 – patricus